Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 7, 2021
1 parent 0f9a947 commit 079705f
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 49 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ jobs:
node-version:
- 14
- 12
- 10
- 8
- 6
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand Down
31 changes: 11 additions & 20 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
declare const sdbm: {
/**
[SDBM](http://www.cse.yorku.ca/~oz/hash.html#sdbm) non-cryptographic hash function.
/**
[SDBM](http://www.cse.yorku.ca/~oz/hash.html#sdbm) non-cryptographic hash function.
@returns The hash as a positive integer.
@returns The hash as a positive integer.
@example
```
import sdbm = require('sdbm');
@example
```
import sdbm from 'sdbm';
sdbm('🦄🌈');
//=> 4053542802
```
*/
(string: string): number;

// TODO: remove this in the next major version, refactor the whole definition to:
// declare function sdbm(string: string): number;
// export = sdbm;
default: typeof sdbm;
};

export = sdbm;
sdbm('🦄🌈');
//=> 4053542802
```
*/
export default function sfbm(string: string): number;
12 changes: 3 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
'use strict';

const sdbm = string => {
export default function sdbm(string) {
let hash = 0;

for (let i = 0; i < string.length; i++) {
hash = string.charCodeAt(i) + (hash << 6) + (hash << 16) - hash;
}

// Convert it to an unsigned 32-bit integer
// Convert it to an unsigned 32-bit integer.
return hash >>> 0;
};

module.exports = sdbm;
// TODO: remove this in the next major version
module.exports.default = sdbm;
}
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expectType} from 'tsd';
import sdbm = require('.');
import sdbm from './index.js';

expectType<number>(sdbm('🦄🌈'));
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
Copyright (c) Sindre Sorhus <[email protected]> (https://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:

Expand Down
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"description": "SDBM non-cryptographic hash function",
"license": "MIT",
"repository": "sindresorhus/sdbm",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=6"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -28,8 +31,8 @@
"function"
],
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.1",
"xo": "^0.24.0"
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
}
}
10 changes: 1 addition & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,24 @@
[SDBM has good distribution and collisions are rare.](https://softwareengineering.stackexchange.com/questions/49550/which-hashing-algorithm-is-best-for-uniqueness-and-speed/145633#145633)


## Install

```
$ npm install sdbm
```


## Usage

```js
const sdbm = require('sdbm');
import sdbm from 'sdbm';

sdbm('🦄🌈');
//=> 4053542802
```

It returns the hash as a positive integer.


## Related

- [fnv1a](https://github.com/sindresorhus/fnv1a) - FNV-1a non-cryptographic hash function
- [djb2a](https://github.com/sindresorhus/djb2a) - DJB2a non-cryptographic hash function


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import sdbm from '.';
import sdbm from './index.js';

test('main', t => {
t.is(sdbm(''), 0);
Expand Down

0 comments on commit 079705f

Please sign in to comment.