Skip to content

Commit

Permalink
chore(dep): add stringify-package to project source
Browse files Browse the repository at this point in the history
  • Loading branch information
Merfoo committed Apr 5, 2023
1 parent bc6f792 commit 5b44502
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 13 deletions.
18 changes: 18 additions & 0 deletions lib/stringify-package.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

module.exports = stringifyPackage

const DEFAULT_INDENT = 2
const CRLF = '\r\n'
const LF = '\n'

function stringifyPackage (data, indent, newline) {
indent = indent || (indent === 0 ? 0 : DEFAULT_INDENT)
const json = JSON.stringify(data, null, indent)

if (newline === CRLF) {
return json.replace(/\n/g, CRLF) + CRLF
}

return json + LF
}
2 changes: 1 addition & 1 deletion lib/updaters/types/json.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const stringifyPackage = require('stringify-package')
const stringifyPackage = require('../../stringify-package')
const detectIndent = require('detect-indent')
const detectNewline = require('detect-newline')

Expand Down
11 changes: 0 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"find-up": "^5.0.0",
"git-semver-tags": "^4.0.0",
"semver": "^7.1.1",
"stringify-package": "^1.0.1",
"yargs": "^17.0.0"
},
"devDependencies": {
Expand Down
39 changes: 39 additions & 0 deletions test/stringify-package.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* global describe it */

'use strict'

const stringifyPackage = require('../lib/stringify-package')

require('chai').should()

describe('stringifyPackage()', function () {
const dummy = { name: 'dummy' }

it('with no params uses \\n', function () {
stringifyPackage(dummy).should.match(/\n$/m)
})

it('uses \\n', function () {
stringifyPackage(dummy, 2, '\n').should.match(/\n$/m)
})

it('uses \\r\\n', function () {
stringifyPackage(dummy, 2, '\r\n').should.match(/\r\n$/m)
})

it('with no params uses 2-space indent', function () {
stringifyPackage(dummy).should.match(/^ {2}"name": "dummy"/m)
})

it('uses 2-space indent', function () {
stringifyPackage(dummy, 2, '\n').should.match(/^ {2}"name": "dummy"/m)
})

it('uses 4-space indent', function () {
stringifyPackage(dummy, 4, '\n').should.match(/^ {4}"name": "dummy"/m)
})

it('0 works', function () {
stringifyPackage(dummy, 0).split(/\r\n|\r|\n/).length.should.equal(2)
})
})

0 comments on commit 5b44502

Please sign in to comment.