Skip to content

Commit e976283

Browse files
committed
first commit
0 parents  commit e976283

18 files changed

+557
-0
lines changed

.bumpedrc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
files: [
2+
'package.json'
3+
]
4+
5+
plugins:
6+
7+
prerelease:
8+
9+
'Linting config files':
10+
plugin: 'bumped-finepack'
11+
12+
postrelease:
13+
14+
'Generating CHANGELOG file':
15+
plugin: 'bumped-changelog'
16+
17+
'Commiting new version':
18+
plugin: 'bumped-terminal'
19+
command: 'git add CHANGELOG.md package.json && git commit -m "Release $newVersion"'
20+
21+
'Detecting problems before publish':
22+
plugin: 'bumped-terminal'
23+
command: 'git-dirty && npm test'
24+
25+
'Publishing tag at GitHub':
26+
plugin: 'bumped-terminal'
27+
command: 'git tag $newVersion && git push && git push --tags'
28+
29+
'Publishing at NPM':
30+
plugin: 'bumped-terminal'
31+
command: 'npm publish'

.editorconfig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# http://editorconfig.org
2+
3+
root = true
4+
5+
[*]
6+
indent_style = space
7+
indent_size = 2
8+
end_of_line = lf
9+
charset = utf-8
10+
trim_trailing_whitespace = true
11+
insert_final_newline = true
12+
max_line_length = 80
13+
indent_brace_style = 1TBS
14+
spaces_around_operators = true
15+
quote_type = auto
16+
17+
[package.json]
18+
indent_style = space
19+
indent_size = 2
20+
21+
[*.md]
22+
trim_trailing_whitespace = false

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.gitignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
############################
2+
# npm
3+
############################
4+
node_modules
5+
npm-debug.log
6+
7+
############################
8+
# tmp, editor & OS files
9+
############################
10+
.tmp
11+
*.swo
12+
*.swp
13+
*.swn
14+
*.swm
15+
.DS_Store
16+
*#
17+
*~
18+
.idea
19+
*sublime*
20+
nbproject
21+
22+
############################
23+
# Tests
24+
############################
25+
testApp
26+
coverage
27+
.nyc_output
28+
29+
############################
30+
# Other
31+
############################
32+
.node_history

.npmignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.idea
2+
.project
3+
*.sublime-*
4+
.DS_Store
5+
*.seed
6+
*.log
7+
*.csv
8+
*.dat
9+
*.out
10+
*.pid
11+
*.swp
12+
*.swo
13+
node_modules
14+
coverage
15+
*.tgz
16+
*.xml

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
unsafe-perm=true

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: node_js
2+
node_js:
3+
- "stable"
4+
- "6"
5+
- "5"
6+
- "4"
7+
- "0.12"
8+
- "0.10"
9+
after_success: npm run coveralls

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright © 2016 Kiko Beats <josefrancisco.verdu@gmail.com> (https://github.com/Kikobeats)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# json-stringify-deterministic
2+
3+
<p align="center">
4+
<br>
5+
<img src="https://i.imgur.com/Mh13XWB.gif" alt="json-stringify-deterministic">
6+
<br>
7+
</p>
8+
9+
![Last version](https://img.shields.io/github/tag/Kikobeats/json-stringify-deterministic.svg?style=flat-square)
10+
[![Build Status](https://img.shields.io/travis/Kikobeats/json-stringify-deterministic/master.svg?style=flat-square)](https://travis-ci.org/Kikobeats/json-stringify-deterministic)
11+
[![Coverage Status](https://img.shields.io/coveralls/Kikobeats/json-stringify-deterministic.svg?style=flat-square)](https://coveralls.io/github/Kikobeats/json-stringify-deterministic)
12+
[![Dependency status](https://img.shields.io/david/Kikobeats/json-stringify-deterministic.svg?style=flat-square)](https://david-dm.org/Kikobeats/json-stringify-deterministic)
13+
[![Dev Dependencies Status](https://img.shields.io/david/dev/Kikobeats/json-stringify-deterministic.svg?style=flat-square)](https://david-dm.org/Kikobeats/json-stringify-deterministic#info=devDependencies)
14+
[![NPM Status](https://img.shields.io/npm/dm/json-stringify-deterministic.svg?style=flat-square)](https://www.npmjs.org/package/json-stringify-deterministic)
15+
[![Donate](https://img.shields.io/badge/donate-paypal-blue.svg?style=flat-square)](https://paypal.me/Kikobeats)
16+
17+
**NOTE:** more badges availables in [shields.io](https://shields.io/)
18+
19+
> deterministic version of JSON.stringify() so you can get a consistent hash from stringified results.
20+
21+
## Install
22+
23+
```bash
24+
$ npm install json-stringify-deterministic --save
25+
```
26+
27+
## Usage
28+
29+
```js
30+
const jsonStringifyDeterministic = require('json-stringify-deterministic')
31+
32+
jsonStringifyDeterministic('do something')
33+
//=> return something
34+
```
35+
36+
## API
37+
38+
### jsonStringifyDeterministic(input, [options])
39+
40+
#### input
41+
42+
*Required*<br>
43+
Type: `string`
44+
45+
Lorem ipsum.
46+
47+
#### options
48+
49+
##### foo
50+
51+
Type: `boolean`
52+
Default: `false`
53+
54+
Lorem ipsum.
55+
56+
## License
57+
58+
MIT © [Kiko Beats](https://github.com/Kikobeats).

index.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
'use strict'
2+
3+
var json = typeof JSON !== 'undefined' ? JSON : require('jsonify')
4+
5+
var isArray = Array.isArray || function (x) {
6+
return {}.toString.call(x) === '[object Array]'
7+
}
8+
9+
var objectKeys = Object.keys || function (obj) {
10+
var has = Object.prototype.hasOwnProperty || function () { return true }
11+
var keys = []
12+
for (var key in obj) {
13+
if (has.call(obj, key)) keys.push(key)
14+
}
15+
return keys
16+
}
17+
18+
function stringify (obj, opts) {
19+
if (!opts) opts = {}
20+
if (typeof opts === 'function') opts = { cmp: opts }
21+
var space = opts.space || ''
22+
if (typeof space === 'number') space = Array(space + 1).join(' ')
23+
var cycles = (typeof opts.cycles === 'boolean') ? opts.cycles : false
24+
var replacer = opts.replacer || function (key, value) { return value; }
25+
26+
var cmp = opts.cmp && (function (f) {
27+
return function (node) {
28+
return function (a, b) {
29+
var aobj = { key: a, value: node[a]}
30+
var bobj = { key: b, value: node[b]}
31+
return f(aobj, bobj)
32+
}
33+
}
34+
})(opts.cmp)
35+
36+
var seen = []
37+
return (function stringify (parent, key, node, level) {
38+
var indent = space ? ('\n' + new Array(level + 1).join(space)) : ''
39+
var colonSeparator = space ? ': ' : ':'
40+
41+
if (node && node.toJSON && typeof node.toJSON === 'function') {
42+
node = node.toJSON()
43+
}
44+
45+
node = replacer.call(parent, key, node)
46+
47+
if (node === undefined) {
48+
return
49+
}
50+
if (typeof node !== 'object' || node === null) {
51+
return json.stringify(node)
52+
}
53+
if (isArray(node)) {
54+
var out = []
55+
for (var i = 0; i < node.length; i++) {
56+
var item = stringify(node, i, node[i], level + 1) || json.stringify(null)
57+
out.push(indent + space + item)
58+
}
59+
return '[' + out.join(',') + indent + ']'
60+
} else {
61+
if (seen.indexOf(node) !== -1) {
62+
if (cycles) return json.stringify('__cycle__')
63+
throw new TypeError('Converting circular structure to JSON')
64+
}
65+
else seen.push(node)
66+
67+
var keys = objectKeys(node).sort(cmp && cmp(node))
68+
var out = []
69+
for (var i = 0; i < keys.length; i++) {
70+
var key = keys[i]
71+
var value = stringify(node, key, node[key], level + 1)
72+
73+
if (!value) continue
74+
75+
var keyValue = json.stringify(key)
76+
+ colonSeparator
77+
+ value
78+
79+
out.push(indent + space + keyValue)
80+
}
81+
seen.splice(seen.indexOf(node), 1)
82+
return '{' + out.join(',') + indent + '}'
83+
}
84+
})({ '': obj }, '', obj, 0)
85+
}
86+
87+
module.exports = stringify

0 commit comments

Comments
 (0)