Skip to content

Commit

Permalink
Merge pull request #14 from smithki/development
Browse files Browse the repository at this point in the history
v0.5.4
  • Loading branch information
smithki authored Feb 11, 2020
2 parents dc6bc83 + bc4f140 commit d5fe832
Show file tree
Hide file tree
Showing 19 changed files with 233 additions and 54 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"workspaces": [
"packages/*"
],
"version": "0.5.3",
"version": "0.5.4",
"npmClient": "yarn",
"useWorkspaces": true
}
30 changes: 15 additions & 15 deletions packages/usable-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "usable-react",
"version": "0.5.3",
"version": "0.5.4",
"description": "Basic React hooks to get any project off the ground.",
"author": "Ian K Smith <[email protected]>",
"license": "MIT",
Expand Down Expand Up @@ -28,19 +28,19 @@
"react": "^16.11.0"
},
"dependencies": {
"@usable-react/use-compare": "^0.5.3",
"@usable-react/use-debounced": "^0.5.3",
"@usable-react/use-deferred-children": "^0.5.3",
"@usable-react/use-dom-event": "^0.5.3",
"@usable-react/use-effect-after-mount": "^0.5.3",
"@usable-react/use-effect-trigger": "^0.5.3",
"@usable-react/use-filter": "^0.5.3",
"@usable-react/use-force-update": "^0.5.3",
"@usable-react/use-hash": "^0.5.3",
"@usable-react/use-hash-compare": "^0.5.3",
"@usable-react/use-initial-render": "^0.5.3",
"@usable-react/use-interval": "^0.5.3",
"@usable-react/use-previous": "^0.5.3",
"@usable-react/use-timer": "^0.5.3"
"@usable-react/use-compare": "^0.5.4",
"@usable-react/use-debounced": "^0.5.4",
"@usable-react/use-deferred-children": "^0.5.4",
"@usable-react/use-dom-event": "^0.5.4",
"@usable-react/use-effect-after-mount": "^0.5.4",
"@usable-react/use-effect-trigger": "^0.5.4",
"@usable-react/use-filter": "^0.5.4",
"@usable-react/use-force-update": "^0.5.4",
"@usable-react/use-hash": "^0.5.4",
"@usable-react/use-hash-compare": "^0.5.4",
"@usable-react/use-initial-render": "^0.5.4",
"@usable-react/use-interval": "^0.5.4",
"@usable-react/use-previous": "^0.5.4",
"@usable-react/use-timer": "^0.5.4"
}
}
4 changes: 2 additions & 2 deletions packages/use-compare/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@usable-react/use-compare",
"version": "0.5.3",
"version": "0.5.4",
"description": "Basic React hooks to get any project off the ground.",
"author": "Ian K Smith <[email protected]>",
"license": "MIT",
Expand All @@ -26,6 +26,6 @@
"react": "^16.11.0"
},
"dependencies": {
"@usable-react/use-previous": "^0.5.3"
"@usable-react/use-previous": "^0.5.4"
}
}
2 changes: 1 addition & 1 deletion packages/use-debounced/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@usable-react/use-debounced",
"version": "0.5.3",
"version": "0.5.4",
"description": "Basic React hooks to get any project off the ground.",
"author": "Ian K Smith <[email protected]>",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions packages/use-deferred-children/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@usable-react/use-deferred-children",
"version": "0.5.3",
"version": "0.5.4",
"description": "Basic React hooks to get any project off the ground.",
"author": "Ian K Smith <[email protected]>",
"license": "MIT",
Expand All @@ -26,6 +26,6 @@
"react": "^16.11.0"
},
"dependencies": {
"@usable-react/use-initial-render": "^0.5.3"
"@usable-react/use-initial-render": "^0.5.4"
}
}
2 changes: 1 addition & 1 deletion packages/use-dom-event/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@usable-react/use-dom-event",
"version": "0.5.3",
"version": "0.5.4",
"description": "Basic React hooks to get any project off the ground.",
"author": "Ian K Smith <[email protected]>",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions packages/use-effect-after-mount/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@usable-react/use-effect-after-mount",
"version": "0.5.3",
"version": "0.5.4",
"description": "Basic React hooks to get any project off the ground.",
"author": "Ian K Smith <[email protected]>",
"license": "MIT",
Expand All @@ -26,6 +26,6 @@
"react": "^16.11.0"
},
"dependencies": {
"@usable-react/use-initial-render": "^0.5.3"
"@usable-react/use-initial-render": "^0.5.4"
}
}
4 changes: 2 additions & 2 deletions packages/use-effect-trigger/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@usable-react/use-effect-trigger",
"version": "0.5.3",
"version": "0.5.4",
"description": "Basic React hooks to get any project off the ground.",
"author": "Ian K Smith <[email protected]>",
"license": "MIT",
Expand All @@ -26,6 +26,6 @@
"react": "^16.11.0"
},
"dependencies": {
"@usable-react/use-compare": "^0.5.3"
"@usable-react/use-compare": "^0.5.4"
}
}
8 changes: 4 additions & 4 deletions packages/use-filter/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@usable-react/use-filter",
"version": "0.5.3",
"version": "0.5.4",
"description": "Basic React hooks to get any project off the ground.",
"author": "Ian K Smith <[email protected]>",
"license": "MIT",
Expand All @@ -26,9 +26,9 @@
"react": "^16.11.0"
},
"dependencies": {
"@usable-react/use-compare": "^0.5.3",
"@usable-react/use-hash": "^0.5.3",
"@usable-react/use-timer": "^0.5.3",
"@usable-react/use-compare": "^0.5.4",
"@usable-react/use-hash": "^0.5.4",
"@usable-react/use-timer": "^0.5.4",
"fuse.js": "3.4.6"
}
}
2 changes: 1 addition & 1 deletion packages/use-force-update/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@usable-react/use-force-update",
"version": "0.5.3",
"version": "0.5.4",
"description": "Basic React hooks to get any project off the ground.",
"author": "Ian K Smith <[email protected]>",
"license": "MIT",
Expand Down
6 changes: 3 additions & 3 deletions packages/use-hash-compare/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@usable-react/use-hash-compare",
"version": "0.5.3",
"version": "0.5.4",
"description": "Basic React hooks to get any project off the ground.",
"author": "Ian K Smith <[email protected]>",
"license": "MIT",
Expand All @@ -26,7 +26,7 @@
"react": "^16.11.0"
},
"dependencies": {
"@usable-react/use-compare": "^0.5.3",
"@usable-react/use-hash": "^0.5.3"
"@usable-react/use-compare": "^0.5.4",
"@usable-react/use-hash": "^0.5.4"
}
}
5 changes: 1 addition & 4 deletions packages/use-hash/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@usable-react/use-hash",
"version": "0.5.3",
"version": "0.5.4",
"description": "Basic React hooks to get any project off the ground.",
"author": "Ian K Smith <[email protected]>",
"license": "MIT",
Expand All @@ -24,8 +24,5 @@
},
"peerDependencies": {
"react": "^16.11.0"
},
"dependencies": {
"object-hash": "2.0.1"
}
}
189 changes: 189 additions & 0 deletions packages/use-hash/src/create-hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/* eslint-disable no-underscore-dangle */

/*
This code is modified from the `object-hash` NPM package. It's a great
package, but due to the way it's bundled for distribution, the entire `crypto`
module is used, even though only a small subset of APIs are required. This
inflates the package size by quite a lot! To reduce the size of the
dependency, the `crypto` module is "tree-shaken" by `microbundle` and only the
essentials of the `object-hash` API are included.
@see https://www.npmjs.com/package/object-hash
@see https://github.com/puleos/object-hash
@see https://github.com/puleos/object-hash/issues/91
*/

import { createHash as nodeCreateHash, Hash } from 'crypto';

/** Check if the given function is a native function */
function isNativeFunction(f: any) {
if (typeof f !== 'function') return false;
const exp = /^function\s+\w*\s*\(\s*\)\s*{\s+\[native code\]\s+}$/i;
return exp.exec(Function.prototype.toString.call(f)) != null;
}

export function createHash<T>(object: T) {
const hashingStream = nodeCreateHash('md5');
const { dispatch } = typeHasher(hashingStream);
dispatch(object);

if (!hashingStream.update) hashingStream.end('');
if (hashingStream.digest) return hashingStream.digest('hex');

const buf = hashingStream.read();
return buf.toString('hex');
}

function typeHasher(writeTo: Hash) {
const context: any[] = [];

const write = (str: string) => {
if (writeTo.update) writeTo.update(str, 'utf8');
writeTo.write(str, 'utf8');
};

const dispatch = (value: any) => {
const type = value === null ? 'null' : typeof value;
(typeHandlers as any)[`_${type}`](value);
};

const typeHandlers = {
_object(object: any) {
const pattern = /\[object (.*)\]/i;
const objString = Object.prototype.toString.call(object);
const execArr = pattern.exec(objString);
const objType = execArr ? execArr[1].toLowerCase() : `unknown:[${objString}]`.toLowerCase();

const objectNumber = context.indexOf(object);
if (objectNumber >= 0) dispatch(`[CIRCULAR:${objectNumber}]`);
context.push(object);

if (typeof Buffer !== 'undefined' && Buffer.isBuffer && Buffer.isBuffer(object)) {
write('buffer:');
write(object.toString('hex'));
}

if (objType !== 'object' && objType !== 'function' && objType !== 'asyncfunction') {
if ((typeHandlers as any)[`_${objType}`]) {
(typeHandlers as any)[`_${objType}`](object);
} else {
write(`[${objType}]`);
}
} else {
const keys = Object.keys(object)
.sort()
.splice(0, 0, 'prototype', '__proto__', 'constructor');
write(`object:${keys.length}:`);

keys.forEach(key => {
dispatch(key);
write(':');
write(',');
});
}
},
_array(arr: any[]) {
write(`array:${arr.length}:`);
arr.forEach(dispatch);
},
_date(date: Date) {
write(`date:${date.toJSON()}`);
},
_symbol(sym: symbol) {
write(`symbol:${sym.toString()}`);
},
_error(err: Error) {
write(`error:${err.toString()}`);
},
_boolean(bool: boolean) {
write(`bool:${bool.toString()}`);
},
_string(string: string) {
write(`string:${string.length}:`);
write(string.toString());
},
_function(fn: Function) {
write('fn:');
if (isNativeFunction(fn)) dispatch('[native]');
else dispatch(fn.toString());
dispatch(`function-name:${String(fn.name)}`);
this._object(fn);
},
_number(number: number) {
write(`number:${number.toString()}`);
},
_xml(xml: any) {
write(`xml:${xml.toString()}`);
},
_null() {
write('Null');
},
_undefined() {
write('Undefined');
},
_regexp(regex: RegExp) {
write(`regex:${regex.toString()}`);
},
_uint8array(arr: Uint8Array) {
write('uint8array:');
dispatch(Array.prototype.slice.call(arr));
},
_uint8clampedarray(arr: Uint8ClampedArray) {
write('uint8clampedarray:');
dispatch(Array.prototype.slice.call(arr));
},
_int8array(arr: Int8Array) {
write('uint8array:');
dispatch(Array.prototype.slice.call(arr));
},
_uint16array(arr: Uint16Array) {
write('uint16array:');
dispatch(Array.prototype.slice.call(arr));
},
_int16array(arr: Int16Array) {
write('uint16array:');
dispatch(Array.prototype.slice.call(arr));
},
_uint32array(arr: Uint32Array) {
write('uint32array:');
dispatch(Array.prototype.slice.call(arr));
},
_int32array(arr: Int32Array) {
write('uint32array:');
dispatch(Array.prototype.slice.call(arr));
},
_float32array(arr: Float32Array) {
write('float32array:');
dispatch(Array.prototype.slice.call(arr));
},
_float64array(arr: Float64Array) {
write('float64array:');
dispatch(Array.prototype.slice.call(arr));
},
_arraybuffer(arr: ArrayBuffer) {
write('arraybuffer:');
dispatch(new Uint8Array(arr));
},
_url(url: URL) {
write(`url:${url.toString()}`);
},
_map(map: Map<any, any>) {
write('map:');
const arr = Array.from(map);
typeHandlers._array(arr);
},
_set(set: Set<any>) {
write('set:');
const arr = Array.from(set);
typeHandlers._array(arr);
},
_blob() {
write('[blob]');
},
_domwindow() {
write('domwindow');
},
};

return { dispatch };
}
Loading

0 comments on commit d5fe832

Please sign in to comment.