Comparing version 0.2.4 to 1.0.0
@@ -22,3 +22,3 @@ 'use strict' | ||
* @param {*} data | ||
* @param {boolean} includeData - return data with id | ||
* @param {boolean} includeData | ||
* | ||
@@ -38,2 +38,6 @@ * @returns {object|string|undefined} | ||
} | ||
// convert Infinity to ∞ | ||
else if (data === Infinity) { | ||
data = '∞' | ||
} | ||
// return undefined for everything other than string | ||
@@ -47,7 +51,7 @@ else if (typeof data !== 'string') { | ||
// calculate hash of data, convert to upper, and get first 128 bits | ||
var id = crypto.createHash('sha256').update(data).digest('hex').toUpperCase().substring(0, 32) | ||
var id = crypto.createHash('sha256').update(data).digest('hex').substring(0, 32) | ||
// return either object with id and data or string id | ||
return includeData | ||
return includeData === true | ||
? {data: data, id: id} | ||
: id | ||
} |
@@ -8,3 +8,3 @@ { | ||
"devDependencies": { | ||
"chai": "3.5.0", | ||
"chai": "4.0.0", | ||
"mocha": "3.4.2" | ||
@@ -22,3 +22,3 @@ }, | ||
}, | ||
"version": "0.2.4" | ||
"version": "1.0.0" | ||
} |
@@ -1,1 +0,89 @@ | ||
# stable-id | ||
# stable-id | ||
Stable ID generates a 128-bit hex string id that is the first 128 bits of an | ||
SHA-256 hash of the input data. | ||
Objects are serialized using | ||
[json-stable-stringify](https://www.npmjs.com/package/json-stable-stringify). | ||
Errors will be thrown on objects that include circular references. | ||
As of version 1.0.0 this string is lower case where previously it was upper | ||
case. | ||
## Stable id usage | ||
const stableId = require('stable-id') | ||
stableId(foo) | ||
## Object stable id | ||
// 7a38bf81f383f69433ad6e900d35b3e2 | ||
stableId({foo: 'bar'}) | ||
## String stable id | ||
// 2c26b46b68ffc68ff99b453c1d304134 | ||
stableId('foo') | ||
## Number stable id | ||
// 5feceb66ffc86f38d952786c6d696c79 | ||
stableId(0) | ||
// 5feceb66ffc86f38d952786c6d696c79 | ||
stableId('0') | ||
Numbers are converted to strings so the stable id for a number will be the | ||
same as the stable id of a string representation of the same number. | ||
NaN and Infinity are not supported and return undefined. | ||
## Boolean stable id | ||
// b5bea41b6c623f7c09f1bf24dcae58eb | ||
stableId(true) | ||
// b5bea41b6c623f7c09f1bf24dcae58eb | ||
stableId('true') | ||
Booleans are converted to strings so the string true and the boolean true will | ||
yield the same id. | ||
## Infinity stable id | ||
// 78d9ce976067aaa5aa9024c17a726c9b | ||
stableId(Infinity) | ||
// 78d9ce976067aaa5aa9024c17a726c9b | ||
stableId('∞') | ||
The number Infinity is converted to the U+221E unicode infinity character. | ||
## Undefined id | ||
// undefined | ||
stableId(undefined) | ||
// undefined | ||
stableId(null) | ||
// undefined | ||
stableId(NaN) | ||
undefined, null, and NaN all return an undefined id. | ||
## Stable id with data | ||
var id = stableId({foo: 'bar'}, true) | ||
id.id // 7a38bf81f383f69433ad6e900d35b3e2 | ||
id.data // {"foo":"bar"} | ||
When called with the second argument === true an object will be returned with | ||
an `id` property which is the hex id and a `data` property which is the exact | ||
string that was used to generate that id. | ||
When storing data this method should be used to insure that the data stored | ||
exactly matches the data that was used to create the id. |
@@ -9,11 +9,11 @@ 'use strict' | ||
it('should return id for object', function () { | ||
assert.equal(stableId({foo: 'bar'}), '7A38BF81F383F69433AD6E900D35B3E2') | ||
assert.equal(stableId({foo: 'bar'}), '7a38bf81f383f69433ad6e900d35b3e2') | ||
}) | ||
it('should return id for string', function () { | ||
assert.equal(stableId('foo'), '2C26B46B68FFC68FF99B453C1D304134') | ||
assert.equal(stableId('foo'), '2c26b46b68ffc68ff99b453c1d304134') | ||
}) | ||
it('should return id for number', function () { | ||
assert.equal(stableId(0), '5FECEB66FFC86F38D952786C6D696C79') | ||
assert.equal(stableId(0), '5feceb66ffc86f38d952786c6d696c79') | ||
}) | ||
@@ -27,4 +27,4 @@ | ||
it('should return id for boolean', function () { | ||
assert.equal(stableId(false), 'FCBCF165908DD18A9E49F7FF27810176') | ||
assert.equal(stableId(true), 'B5BEA41B6C623F7C09F1BF24DCAE58EB') | ||
assert.equal(stableId(false), 'fcbcf165908dd18a9e49f7ff27810176') | ||
assert.equal(stableId(true), 'b5bea41b6c623f7c09f1bf24dcae58eb') | ||
}) | ||
@@ -37,2 +37,7 @@ | ||
it('should use U+221E for Infinity', function () { | ||
assert.equal(stableId('∞'), '78d9ce976067aaa5aa9024c17a726c9b') | ||
assert.strictEqual(stableId(Infinity), stableId('∞')) | ||
}) | ||
it('should return undefined for everything else', function () { | ||
@@ -42,3 +47,2 @@ assert.strictEqual(stableId(undefined), undefined) | ||
assert.strictEqual(stableId(NaN), undefined) | ||
assert.strictEqual(stableId(Infinity), undefined) | ||
}) | ||
@@ -59,3 +63,3 @@ | ||
// test id and data | ||
assert.equal(id.id, '7A38BF81F383F69433AD6E900D35B3E2') | ||
assert.equal(id.id, '7a38bf81f383f69433ad6e900d35b3e2') | ||
assert.equal(id.data, '{"foo":"bar"}') | ||
@@ -62,0 +66,0 @@ }) |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
8431
106
1
89