Skip to content

Commit

Permalink
Require Node.js 8, add TypeScript definition (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed May 4, 2019
1 parent 926c7b2 commit bbbfeff
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
[*.yml]
indent_style = space
indent_size = 2
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ node_js:
- '12'
- '10'
- '8'
- '6'
23 changes: 23 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// <reference types="node"/>

/**
Create a MD5 hash with hex encoding.
@param data - Prefer buffers as they're faster to hash, but strings can be useful for small things.
Pass an array instead of concatenating strings and/or buffers. The output is the same, but arrays do not incur the overhead of concatenation.
@example
```
import * as fs from 'fs';
import md5Hex = require('md5-hex');
const buffer = fs.readFileSync('unicorn.png');
md5Hex(buffer);
//=> '1abcb33beeb811dca15f0ac3e47b88d9'
```
*/
declare function md5Hex(data: Buffer | string | ReadonlyArray<Buffer | string>): string;

export = md5Hex;
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
'use strict';
const crypto = require('crypto');

module.exports = function (input) {
module.exports = function (data) {
const hash = crypto.createHash('md5');

const update = buf => {
const inputEncoding = typeof buf === 'string' ? 'utf8' : undefined;
hash.update(buf, inputEncoding);
const update = buffer => {
const inputEncoding = typeof buffer === 'string' ? 'utf8' : undefined;
hash.update(buffer, inputEncoding);
};

if (arguments.length > 1) {
throw new Error('Too many arguments. Try specifying an array.');
}

if (Array.isArray(input)) {
input.forEach(update);
if (Array.isArray(data)) {
data.forEach(update);
} else {
update(input);
update(data);
}

return hash.digest('hex');
Expand Down
11 changes: 11 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {expectType} from 'tsd';
import * as fs from 'fs';
import md5Hex = require('.');

const buffer = fs.readFileSync('unicorn.png');

expectType<string>(md5Hex(buffer));
expectType<string>(md5Hex([buffer]));
expectType<string>(md5Hex('foo'));
expectType<string>(md5Hex(['foo']));
expectType<string>(md5Hex([buffer, 'foo']));
20 changes: 4 additions & 16 deletions license
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
The MIT License (MIT)
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
77 changes: 40 additions & 37 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
{
"name": "md5-hex",
"version": "2.0.0",
"description": "Create a MD5 hash with hex encoding",
"license": "MIT",
"repository": "sindresorhus/md5-hex",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js",
"browser.js"
],
"keywords": [
"hash",
"crypto",
"md5",
"hex",
"buffer",
"browser",
"browserify"
],
"dependencies": {
"md5-o-matic": "^0.1.1"
},
"devDependencies": {
"ava": "*",
"xo": "*"
},
"browser": "browser.js"
"name": "md5-hex",
"version": "2.0.0",
"description": "Create a MD5 hash with hex encoding",
"license": "MIT",
"repository": "sindresorhus/md5-hex",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts",
"browser.js"
],
"keywords": [
"hash",
"crypto",
"md5",
"hex",
"buffer",
"browser",
"browserify"
],
"dependencies": {
"md5-o-matic": "^0.1.1"
},
"devDependencies": {
"@types/node": "^12.0.0",
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
},
"browser": "browser.js"
}
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Checkout [`hasha`](https://github.com/sindresorhus/hasha) if you need something
## Install

```
$ npm install --save md5-hex
$ npm install md5-hex
```


Expand All @@ -30,11 +30,11 @@ md5Hex(buffer);

## API

### md5Hex(input)
### md5Hex(data)

#### input
#### data

Type: `Buffer` `string` `Buffer[]` `string[]`
Type: `Buffer | string | Array<Buffer | string>`

Prefer buffers as they're faster to hash, but strings can be useful for small things.

Expand Down

0 comments on commit bbbfeff

Please sign in to comment.