node-object-hash
Advanced tools
Comparing version 1.1.6 to 1.2.0
# Changelog | ||
## v1.1.X -> 1.2.X | ||
Sorter refactoring and performance improvements | ||
- Added typed arrays support | ||
- Added primitive type constructors support | ||
- Add more docs about type mapping and type coercion | ||
## v1.0.X -> v1.1.X | ||
@@ -4,0 +12,0 @@ |
@@ -15,20 +15,38 @@ 'use strict'; | ||
function _guessObjectType(obj) { | ||
var hasMapSet = typeof Map !== 'undefined'; | ||
if (obj === null) { | ||
return 'null'; | ||
} | ||
if (Array.isArray(obj)) { | ||
return 'array'; | ||
switch (obj.constructor.name) { | ||
case 'Array': | ||
case 'Int8Array': | ||
case 'Uint8Array': | ||
case 'Uint8ClampedArray': | ||
case 'Int16Array': | ||
case 'Uint16Array': | ||
case 'Int32Array': | ||
case 'Uint32Array': | ||
case 'Float32Array': | ||
case 'Float64Array': | ||
case 'Buffer': | ||
return 'array'; | ||
case 'Map': | ||
case 'WeakMap': | ||
return 'map'; | ||
case 'Set': | ||
case 'WeakSet': | ||
return 'set'; | ||
case 'Date': | ||
return 'date'; | ||
case 'String': | ||
return 'string'; | ||
case 'Number': | ||
return 'number'; | ||
case 'Boolean': | ||
return 'boolean'; | ||
case 'Object': | ||
return 'object'; | ||
default: | ||
return 'unknown'; | ||
} | ||
if (hasMapSet && (obj instanceof Map || obj instanceof WeakMap)) { | ||
return 'map'; | ||
} | ||
if (hasMapSet && (obj instanceof Set || obj instanceof WeakSet)) { | ||
return 'set'; | ||
} | ||
if (obj instanceof Date) { | ||
return 'date'; | ||
} | ||
return 'object'; | ||
} | ||
@@ -75,5 +93,5 @@ | ||
sort = typeof options.sort === 'undefined' ? true : options.sort, | ||
self = {}; | ||
stringifier = {}; | ||
self.string = function sortString(obj) { | ||
stringifier.string = function sortString(obj) { | ||
if (coerce) { | ||
@@ -85,3 +103,3 @@ return obj; | ||
self.number = function sortNumber(obj) { | ||
stringifier.number = function sortNumber(obj) { | ||
if (coerce) { | ||
@@ -93,14 +111,14 @@ return obj.toString(); | ||
self.boolean = function sortBoolean(obj) { | ||
stringifier.boolean = function sortBoolean(obj) { | ||
if (coerce) { | ||
return obj ? '1' : '0'; | ||
return obj.valueOf() ? '1' : '0'; | ||
} | ||
return obj ? '<:b>:true' : '<:b>:false'; | ||
return obj.valueOf() ? '<:b>:true' : '<:b>:false'; | ||
}; | ||
self.symbol = function sortSymbol() { | ||
stringifier.symbol = function sortSymbol() { | ||
return '<:smbl>'; | ||
}; | ||
self.undefined = function sortUndefined() { | ||
stringifier.undefined = function sortUndefined() { | ||
if (coerce) { | ||
@@ -112,3 +130,3 @@ return ''; | ||
self.null = function sortNull() { | ||
stringifier.null = function sortNull() { | ||
if (coerce) { | ||
@@ -120,3 +138,3 @@ return ''; | ||
self.function = function sortFunction(obj) { | ||
stringifier.function = function sortFunction(obj) { | ||
if (coerce) { | ||
@@ -128,3 +146,3 @@ return obj.name + '=>' + obj.toString(); | ||
self.array = function sortArray(obj) { | ||
stringifier.array = function sortArray(obj) { | ||
var item, | ||
@@ -137,3 +155,3 @@ itemType, | ||
itemType = _guessType(item); | ||
result.push(self[itemType](item)); | ||
result.push(stringifier[itemType](item)); | ||
} | ||
@@ -144,7 +162,7 @@ | ||
self.set = function sortSet(obj) { | ||
return self.array(Array.from(obj)); | ||
stringifier.set = function sortSet(obj) { | ||
return stringifier.array(Array.from(obj)); | ||
}; | ||
self.date = function sortDate(obj) { | ||
stringifier.date = function sortDate(obj) { | ||
var dateStr = obj.toISOString(); | ||
@@ -158,3 +176,3 @@ | ||
self.object = function sortObject(obj) { | ||
stringifier.object = function sortObject(obj) { | ||
var keys = sort ? Object.keys(obj).sort() : Object.keys(obj), | ||
@@ -169,3 +187,3 @@ objArray = [], | ||
valueType = _guessType(value); | ||
objArray.push(key + ':' + self[valueType](value)); | ||
objArray.push(key + ':' + stringifier[valueType](value)); | ||
} | ||
@@ -175,3 +193,3 @@ return '{' + objArray.toString() + '}'; | ||
self.map = function sortMap(obj) { | ||
stringifier.map = function sortMap(obj) { | ||
var arr = Array.from(obj), | ||
@@ -186,4 +204,4 @@ key, value, item, | ||
item = [ | ||
self[_guessType(key)](key), | ||
self[_guessType(value)](value) | ||
stringifier[_guessType(key)](key), | ||
stringifier[_guessType(value)](value) | ||
]; | ||
@@ -196,2 +214,6 @@ arr[i] = item; | ||
stringifier.unknown = function unknownToString(obj) { | ||
return '<:' + obj.constructor.name + '>:' + obj.toString(); | ||
}; | ||
/** | ||
@@ -204,3 +226,3 @@ * Object sorting function | ||
function objectToString(obj) { | ||
return self[_guessType(obj)](obj); | ||
return stringifier[_guessType(obj)](obj); | ||
} | ||
@@ -207,0 +229,0 @@ |
{ | ||
"name": "node-object-hash", | ||
"version": "1.1.6", | ||
"version": "1.2.0", | ||
"description": "Node.js object hash library with properties/arrays sorting to provide constant hashes", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -19,6 +19,3 @@ # node-object-hash | ||
- Supports ES6 (Weak)Maps and (Weak)Sets. | ||
- Supports type coercion (e.g. 1 and "1" will be the same) | ||
- rules: | ||
- numbers and strings represented without quotes; | ||
- boolean values converted to numbers; | ||
- Supports type coercion (see table below) | ||
- Supports all hashes and encodings of crypto library | ||
@@ -28,2 +25,58 @@ - Supports large objects and arrays | ||
## Type map | ||
This map displays what types will have identical string representation (e.g. new Set([1, 2, 3]) and [1, 2, 3] will have | ||
equal string representations and hashes. | ||
| Initial type | Mapped type | | ||
|---------------------------|--------------| | ||
| Array ([]) | array | | ||
| ArrayObject (new Array()) | | | ||
| Int8Array | | | ||
| Uint8Array | | | ||
| Uint8ClampedArray | | | ||
| Int16Array | | | ||
| Uint16Array | | | ||
| Int32Array | | | ||
| Uint32Array | | | ||
| Float32Array | | | ||
| Float64Array | | | ||
| Buffer | | | ||
| Set | | | ||
| WeakSet | | | ||
| | | | ||
| string ('') | string | string | | ||
| String (new String()) | | | ||
| | | | ||
| boolean (true) | boolean | | ||
| Boolean (new Boolean()) | | | ||
| | | | ||
| number (true) | number | | ||
| Number (new Number()) | | | ||
| | | | ||
| Date | date | | ||
| | | | ||
| Symbol | symbol | | ||
| | | | ||
| undefined | undefined | | ||
| | | | ||
| null | null | | ||
| | | | ||
| function | function | | ||
| | | | ||
| Object ({}) | object | | ||
| Object (new Object()) | | | ||
| | | | ||
| other | unknown | | ||
## Coercion map | ||
| Initial "type" | Coerced type | Example | | ||
|----------------|----------------|--------------| | ||
| boolean | string | true -> 1 | | ||
| number | string | '1' -> 1 | | ||
| string | string | 'a' -> a | | ||
| null | string (empty) | null -> | | ||
| undefined | string (empty) | undefined -> | | ||
# Changes | ||
@@ -344,2 +397,3 @@ | ||
| node-object-hash-1.1.X (node v7) | 2555.583 | 27 | | ||
| node-object-hash-1.2.X (node v7) | 2390.752 | 28 | | ||
| object-hash-1.1.5 (node v7) | 28115.553 | 39 | | ||
@@ -352,8 +406,9 @@ | object-hash-1.1.4 | 534528.254 | 41 | | ||
``` | ||
node-object-hash x 844 ops/sec ±2.51% (82 runs sampled) | ||
node-object-hash-v0 x 540 ops/sec ±1.34% (82 runs sampled) | ||
hash-object x 310 ops/sec ±0.88% (81 runs sampled) | ||
object-hash x 107 ops/sec ±1.66% (72 runs sampled) | ||
``` | ||
| Library (node v7) | Perf (ops/s) | | ||
|------------------------|--------------| | ||
| node-object-hash-0.2.1 | 540 ±1.34% | | ||
| node-object-hash-1.1.X | 844 ±2.51% | | ||
| node-object-hash-1.2.X | 1021 ±1.81% | | ||
| object-hash-1.1.5 | 106 ±0.88% | | ||
| hash-object-0.1.7 | 305 ±1.66% | | ||
@@ -360,0 +415,0 @@ ## Links |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49034
10
398
419