asset-pipe-common
Advanced tools
Comparing version 1.0.0-alpha.2 to 1.0.0-alpha.3
@@ -70,2 +70,17 @@ "use strict"; | ||
/** | ||
* Creates a hash out of a String or Buffer | ||
* | ||
* @param {String|Buffer} source Source to make hash out of | ||
* | ||
* @returns {String} a hash | ||
*/ | ||
module.exports.hasher = (source) => { | ||
const hasher = crypto.createHash('sha1'); | ||
return hasher.update(source, Buffer.isBuffer(source) ? null : 'utf8').digest('hex'); | ||
}; | ||
/** | ||
* Constructs a temporary filename | ||
@@ -72,0 +87,0 @@ * |
{ | ||
"name": "asset-pipe-common", | ||
"version": "1.0.0-alpha.2", | ||
"version": "1.0.0-alpha.3", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Trygve Lie", |
@@ -35,5 +35,28 @@ "use strict"; | ||
hasher.on('finish', () => { | ||
console.log(hasher.hash); | ||
// console.log(hasher.hash); | ||
t.end(); | ||
}); | ||
}); | ||
tap.test('hasher() - hash a String - should match given hash', (t) => { | ||
t.equal(lib.hasher('abc'), 'a9993e364706816aba3e25717850c26c9cd0d89d'); | ||
t.equal(lib.hasher('abce'), '0a431a7631cabf6b11b984a943127b5e0aa9d687'); | ||
t.end(); | ||
}); | ||
tap.test('hasher() - hash a Buffer - should match given hash', (t) => { | ||
t.equal(lib.hasher(new Buffer('abc')), 'a9993e364706816aba3e25717850c26c9cd0d89d'); | ||
t.equal(lib.hasher(new Buffer('abc').toString()), 'a9993e364706816aba3e25717850c26c9cd0d89d'); | ||
t.equal(lib.hasher(new Buffer('abce')), '0a431a7631cabf6b11b984a943127b5e0aa9d687'); | ||
t.equal(lib.hasher(new Buffer('abce').toString()), '0a431a7631cabf6b11b984a943127b5e0aa9d687'); | ||
t.end(); | ||
}); | ||
tap.test('hasher() - source contains hexadecimal escape sequence - should match given hash', (t) => { | ||
t.equal(lib.hasher('ab\xff'), 'ba5142a8207bd61baddf325088732e71cbfe8eb6'); | ||
t.equal(lib.hasher(new Buffer('ab\xff')), 'ba5142a8207bd61baddf325088732e71cbfe8eb6'); | ||
t.equal(lib.hasher(new Buffer('ab\xff').toString()), 'ba5142a8207bd61baddf325088732e71cbfe8eb6'); | ||
t.end(); | ||
}); |
667107
2137