Coder Social home page Coder Social logo

sindresorhus / fnv1a Goto Github PK

View Code? Open in Web Editor NEW
196.0 5.0 13.0 29 KB

FNV-1a non-cryptographic hash function

License: MIT License

JavaScript 98.87% TypeScript 1.13%
fnv-1a fnv fnv-algorithms non-cryptographic-hash-functions node-module npm-package

fnv1a's Introduction

fnv1a

FNV-1a non-cryptographic hash function

FNV-1a has outstanding distribution and collisions are rare.

FNV hashes are designed to be fast while maintaining a low collision rate. The FNV speed allows one to quickly hash lots of data while maintaining a reasonable collision rate. The high dispersion of the FNV hashes makes them well suited for hashing nearly identical strings such as URLs, hostnames, filenames, text, IP addresses, etc.

Install

npm install @sindresorhus/fnv1a

Usage

import fnv1a from '@sindresorhus/fnv1a';

fnv1a('๐Ÿฆ„๐ŸŒˆ', {size: 32});
//=> 2868248295n

fnv1a('๐Ÿฆ„๐ŸŒˆ', {size: 128});
//=> 13487074350300261116944693128525960095n

Number(fnv1a('๐Ÿฆ„๐ŸŒˆ', {size: 32}));
//=> 2868248295

const bytes = new Uint8Array([240, 159, 166, 132, 240, 159, 140, 136]);
fnv1a(bytes, {size: 32});
//=> 2868248295n

API

fnv1a(value, options?)

Returns the hash as a positive BigInt.

If you need it as a number, use 32 as size and wrap the return value in Number(โ€ฆ).

value

Type: string | Uint8Array

A string or UTF-8 bytes.

options

Type: object

size

Type: number
Values: 32 | 64 | 128 | 256 | 512 | 1024
Default: 32

The bit size of the hash.

utf8Buffer

Type: Uint8Array

A Uint8Array used to encode the string into UTF-8 bytes.

This array can be reused across calls to fnv1a. Doing so will improve performance because it avoids allocating a new Uint8Array when encoding the string.

The size of the array does not have to be large enugh to hold the entire string, but performance will be improved if it is.

This option is only used when value is a string.

import fnv1a from '@sindresorhus/fnv1a';

const utf8Buffer = new Uint8Array(100);

fnv1a('๐Ÿฆ„๐ŸŒˆ', {size: 32, utf8Buffer});
//=> 2868248295n

Related

  • djb2a - DJB2a non-cryptographic hash function
  • sdbm - SDBM non-cryptographic hash function

fnv1a's People

Contributors

bendingbender avatar broofa avatar dasprid avatar dbrockman avatar fregante avatar richienb avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

fnv1a's Issues

Error [ERR_REQUIRE_ESM] & TypeError: Unknown file extension ".ts" with latest version 3.0.0

When I try to build my typescript code and run it I am getting the following error.

const fnv1a_1 = __importDefault(require("@sindresorhus/fnv1a"));
                                ^

Error [ERR_REQUIRE_ESM]: require() of ES Module node_modules\@sindresorhus\fnv1a\index.js from dist\utils\load-balancer.js not supported.
Instead change the require of index.js in dist\utils\load-balancer.js to a dynamic import() which is available in all CommonJS modules.

My tsconfig.json config is,

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "commonjs",
    "lib": ["ES2020"],
    "types": ["reflect-metadata", "mocha"],
    "strict": true,
    "sourceMap": true,
    "outDir": "dist",
    "moduleResolution": "node",
    "removeComments": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "resolveJsonModule": true,
    "baseUrl": "."
  },
  "exclude": ["node_modules"],
  "include": ["src/**/*"]
}

Surprisingly my test cases are also not able tp run. Mocha runner fails with following error,

TypeError: Unknown file extension ".ts" for tests\end-to-end\change-notification.test.ts
    at new NodeError (node:internal/errors:371:5)
    at Object.file: (node:internal/modules/esm/get_format:72:15)
    at defaultGetFormat (node:internal/modules/esm/get_format:85:38)
    at defaultLoad (node:internal/modules/esm/load:13:42)
    at ESMLoader.load (node:internal/modules/esm/loader:303:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:230:58)
    at new ModuleJob (node:internal/modules/esm/module_job:63:26)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:244:11)
    at async Promise.all (index 0)
    at ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
    at formattedImport (node_modules\mocha\lib\esm-utils.js:7:14)
    at Object.exports.loadFilesAsync (node_modules\mocha\lib\esm-utils.js:55:20)
    at singleRun (node_modules\mocha\lib\cli\run-helpers.js:125:3)
    at Object.exports.handler (node_modules\mocha\lib\cli\run.js:362:5)

Note - The same codebase with no changes in the code works absolutely fine with @sindresorhus/fnv1a version 2.0.1
node - v16.13.0 LTS
npm - v8.1.0

Any idea as to why is it breaking in runtime & breaking my tests? My tsc compilation works fine!

@sindresorhus

Is this library intended to be used only within nodejs?

Hi @sindresorhus, this is more of a question for understanding than an issue.

I've encountered at least one other OSS project, https://github.com/nearform/graphql-hooks (specifically, the https://github.com/nearform/graphql-hooks/tree/master/packages/graphql-hooks-memcache package), that is using your v2.0.0 of your package, but within both nodejs AND browser. This got me thinking towards the intent of this library - is it intended to be used only within nodejs?

If so, then that means maintainers of other projects aren't using your package correctly then, and this may cause/allow them to then do so. Would first like to gain this understanding though.

Support higher bit variants

Issuehunt badges

FNV1a can be done in 32-, 64-, 128-, 256-, 512-, and 1024-bit flavors. Currently only 32-bit is implemented, because of JS integer limitations. Now that we have BigInt in JS, we can implement the other flavors.

More info: http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-source

PR welcome :)


IssueHunt Summary

shynrou shynrou has been rewarded.

Backers (Total: $60.00)

Submitted pull Requests


Tips


IssueHunt has been backed by the following sponsors. Become a sponsor

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.