Skip to content

Commit

Permalink
fix(parse): make parse return a 'deep' clone
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-bompart committed Apr 10, 2022
1 parent fd67a71 commit 428a728
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion functions/parse.js
Expand Up @@ -7,7 +7,7 @@ const parse = (version, options) => {
options = parseOptions(options)

if (version instanceof SemVer) {
return version
return new SemVer(version.version)
}

if (typeof version !== 'string') {
Expand Down
11 changes: 9 additions & 2 deletions test/functions/parse.js
Expand Up @@ -11,8 +11,15 @@ t.test('returns null instead of throwing when presented with garbage', t => {

t.test('parse a version into a SemVer object', t => {
t.match(parse('1.2.3'), new SemVer('1.2.3'))
const s = new SemVer('4.5.6')
t.equal(parse(s), s, 'just return it if its a SemVer obj')
const inputSemVer = new SemVer('4.5.6')
const parsedSemVer = parse(inputSemVer)
t.equal(
JSON.stringify(parse(inputSemVer)),
JSON.stringify(inputSemVer),
'returns a copy if input is a SemVer obj'
)
inputSemVer.version = '4.5.7'
t.equal(parsedSemVer.version, '4.5.6', 'is a deep clone when input is a SemVer obj')
const loose = new SemVer('4.2.0', { loose: true })
t.match(parse('4.2.0', true), loose, 'looseness as a boolean')
t.match(parse('4.2.0', { loose: true }), loose, 'looseness as an option')
Expand Down

0 comments on commit 428a728

Please sign in to comment.