Skip to content

Commit

Permalink
update README
Browse files Browse the repository at this point in the history
  • Loading branch information
planttheidea committed Dec 31, 2022
1 parent b24c75d commit e4d3025
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 101 deletions.
15 changes: 11 additions & 4 deletions DEV_ONLY/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'regenerator-runtime/runtime';

import React from 'react';
import { render } from 'react-dom';
import { createRoot } from 'react-dom/client';
import hashIt from '../src';
import hash from '../src';

document.body.style.backgroundColor = '#1d1d1d';
Expand Down Expand Up @@ -230,8 +229,16 @@ const visualValidation = () => {
visualValidation();
// hashOnlyValidation();

const promise = Promise.resolve(123);

console.log(hash(promise));
console.log(hash(promise));
console.log(hash(Promise.resolve(123)));

const div = document.createElement('div');

render(<div>Check the console for more details!</div>, div);
const root = createRoot(div);

document.body.appendChild(div);

root.render(<div>Check the console for more details!</div>);
86 changes: 12 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ Fast and consistent hashCode for any object type
- [Table of contents](#table-of-contents)
- [Usage](#usage)
- [Overview](#overview)
- [Equality methods](#equality-methods)
- [hash.is](#hashis)
- [hash.is.all](#hashisall)
- [hash.is.any](#hashisany)
- [hash.is.not](#hashisnot)
- [Note](#note)
- [Support](#support)
- [Browsers](#browsers)
Expand Down Expand Up @@ -54,9 +49,19 @@ Well ... sadly, no, there are a few exceptions.
- `WeakMap` / `WeakSet`
- The spec explicitly forbids iteration over them, so the unique values cannot be discovered

In each of these cases, no matter what the values of the object, they will always yield the same hash result, which is unique to each object type. If you have any ideas about how these can be uniquely hashed, they are welcome!
For each of these object types, the object will have a unique hash based on the object reference itself:

Here is the list of object classes that produce unique hashes:
```ts
const promise = Promise.resolve(123);

console.log(hash(promise)); // 16843037491939
console.log(hash(promise)); // 16843037491939
console.log(hash(Promise.resolve(123))); // 4622327363876
```

Notice even if the internal values of the object are the same, the hash is different. This is because the values of the above object types cannot be introspected.

Here is the list of object classes that produce consistent, unique hashes based on their value:

- `Arguments`
- `Array`
Expand Down Expand Up @@ -100,73 +105,6 @@ Here is the list of object classes that produce unique hashes:

If there is an object class or data type that is missing, please submit an issue.

## Equality methods

### hash.is

`hash.is(value1: any, value2: any): boolean`

Compares the two objects to determine equality.

```javascript
console.log(hash.is(null, 123)); // false
console.log(hash.is(null, null)); // true
```

### hash.is.all

`hash.is.all(value1: any, value2: any[, value3: any[, ...valueN]]): boolean`

Compares the first object to all other objects passed to determine if all are equal based on hashCode

```javascript
const foo = {
foo: 'bar',
};
const alsoFoo = {
foo: 'bar',
};
const stillFoo = {
foo: 'bar',
};

console.log(hash.is.all(foo, alsoFoo)); // true
console.log(hash.is.all(foo, alsoFoo, stillFoo)); // true
```

### hash.is.any

`hash.is.any(value1: any, value2: any[, value3: any[, ...valueN]]): boolean`

Compares the first object to all other objects passed to determine if any are equal based on hashCode

```javascript
const foo = {
foo: 'bar',
};
const alsoFoo = {
foo: 'bar',
};
const nopeBar = {
bar: 'baz',
};

console.log(hash.is.any(foo, alsoFoo)); // true
console.log(hash.is.any(foo, nopeBar)); // false
console.log(hash.is.any(foo, alsoFoo, nopeBar)); // true
```

### hash.is.not

`hash.is.not(value1: any, value2: any): boolean`

Compares the two objects to determine non-equality.

```javascript
console.log(hash.is.not(null, 123)); // true
console.log(hash.is.not(null, null)); // false
```

## Note

While the hashes will be consistent when calculated within the same environment, there is no guarantee that the resulting hash will be the same across different environments due to environment-specific or browser-specific implementations of features. This is limited to extreme edge cases, such as hashing the `window` object, but should be considered if being used with persistence over different environments.
Expand Down
35 changes: 17 additions & 18 deletions build/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable @typescript-eslint/no-var-requires */

import ESLintWebpackPlugin from "eslint-webpack-plugin";
import HtmlWebpackPlugin from "html-webpack-plugin";
import path from "path";
import ESLintWebpackPlugin from 'eslint-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import path from 'path';
import { fileURLToPath } from 'url';
import webpack from 'webpack';

Expand All @@ -13,46 +13,45 @@ export default {
cache: true,

devServer: {
host: "localhost",
host: 'localhost',
port: PORT,
},

devtool: "source-map",
devtool: 'source-map',

entry: [path.resolve(ROOT, "DEV_ONLY", "index.tsx")],
entry: [path.resolve(ROOT, 'DEV_ONLY', 'index.tsx')],

mode: "development",
mode: 'development',

module: {
rules: [
{
include: [path.resolve(ROOT, "src"), path.resolve(ROOT, "DEV_ONLY")],
loader: "babel-loader",
include: [path.resolve(ROOT, 'src'), path.resolve(ROOT, 'DEV_ONLY')],
loader: 'ts-loader',
options: {
cacheDirectory: true,
presets: ["@babel/preset-react"],
reportFiles: ['src/*.{ts|tsx}'],
},
test: /\.(js|ts|tsx)$/,
test: /\.(ts|tsx)$/,
},
],
},

output: {
filename: "moize.js",
library: "moize",
libraryTarget: "umd",
path: path.resolve(ROOT, "dist"),
filename: 'hash-it.js',
library: 'hashIt',
libraryTarget: 'umd',
path: path.resolve(ROOT, 'dist'),
publicPath: `http://localhost:${PORT}/`,
umdNamedDefine: true,
},

plugins: [
new ESLintWebpackPlugin(),
new webpack.EnvironmentPlugin(["NODE_ENV"]),
new webpack.EnvironmentPlugin(['NODE_ENV']),
new HtmlWebpackPlugin(),
],

resolve: {
extensions: [".tsx", ".ts", ".js"],
extensions: ['.tsx', '.ts', '.js'],
},
};
1 change: 0 additions & 1 deletion jest.init.js

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"rimraf": "^3.0.2",
"rollup": "^3.9.0",
"ts-jest": "^29.0.3",
"ts-loader": "^9.4.2",
"tslib": "^2.4.1",
"typescript": "^4.9.4",
"webpack": "^5.75.0",
Expand Down
18 changes: 14 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1870,7 +1870,7 @@ chalk@^2.0.0, chalk@^2.0.1:
escape-string-regexp "^1.0.5"
supports-color "^5.3.0"

chalk@^4.0.0:
chalk@^4.0.0, chalk@^4.1.0:
version "4.1.2"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01"
integrity sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==
Expand Down Expand Up @@ -2463,7 +2463,7 @@ encodeurl@~1.0.2:
resolved "https://registry.yarnpkg.com/encodeurl/-/encodeurl-1.0.2.tgz#ad3ff4c86ec2d029322f5a02c3a9a606c95b3f59"
integrity sha512-TPJXq8JqFaVYm2CWmPvnP2Iyo4ZSM7/QKcSmuMLDObfpH5fi7RUGmd/rTDf+rut/saiDiQEeVTNgAmJEdAOx0w==

enhanced-resolve@^5.10.0:
enhanced-resolve@^5.0.0, enhanced-resolve@^5.10.0:
version "5.12.0"
resolved "https://registry.yarnpkg.com/enhanced-resolve/-/enhanced-resolve-5.12.0.tgz#300e1c90228f5b570c4d35babf263f6da7155634"
integrity sha512-QHTXI/sZQmko1cbDoNAa3mJ5qhWUUNAq3vR0/YiD379fWQrcfuoX1+HW2S0MTt7XmoPLapdaDKUtelUSPic7hQ==
Expand Down Expand Up @@ -4665,7 +4665,7 @@ methods@~1.1.2:
resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"
integrity sha512-iclAHeNqNm68zFtnZ0e+1L2yUIdvzNoauKU4WBA3VvH/vPFieF7qfRlwUZU+DA9P9bPXIS90ulxoUoCH23sV2w==

micromatch@^4.0.2, micromatch@^4.0.4, micromatch@^4.0.5:
micromatch@^4.0.0, micromatch@^4.0.2, micromatch@^4.0.4, micromatch@^4.0.5:
version "4.0.5"
resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-4.0.5.tgz#bc8999a7cbbf77cdc89f132f6e467051b49090c6"
integrity sha512-DMy+ERcEW2q8Z2Po+WNXuw3c5YaUSFjAO5GsJqfEl7UjvtIuFKO6ZrKvcItdy98dwFI2N1tg3zNIdKaQT+aNdA==
Expand Down Expand Up @@ -5724,7 +5724,7 @@ semver-diff@^4.0.0:
dependencies:
semver "^7.3.5"

semver@7.3.8, semver@7.x, semver@^7.3.7:
semver@7.3.8, semver@7.x, semver@^7.3.4, semver@^7.3.7:
version "7.3.8"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.8.tgz#07a78feafb3f7b32347d725e33de7e2a2df67798"
integrity sha512-NB1ctGL5rlHrPJtFDVIVzTyQylMLu9N9VICA6HSFJo8MCGVTMW6gfpicwKmmK/dAjTOrqu5l63JJOpDSrAis3A==
Expand Down Expand Up @@ -6221,6 +6221,16 @@ ts-jest@^29.0.3:
semver "7.x"
yargs-parser "^21.0.1"

ts-loader@^9.4.2:
version "9.4.2"
resolved "https://registry.yarnpkg.com/ts-loader/-/ts-loader-9.4.2.tgz#80a45eee92dd5170b900b3d00abcfa14949aeb78"
integrity sha512-OmlC4WVmFv5I0PpaxYb+qGeGOdm5giHU7HwDDUjw59emP2UYMHy9fFSDcYgSNoH8sXcj4hGCSEhlDZ9ULeDraA==
dependencies:
chalk "^4.1.0"
enhanced-resolve "^5.0.0"
micromatch "^4.0.0"
semver "^7.3.4"

tslib@^1.8.1:
version "1.14.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
Expand Down

0 comments on commit e4d3025

Please sign in to comment.