klona
Advanced tools
Comparing version 1.1.2 to 2.0.0
@@ -1,2 +0,2 @@ | ||
module.exports = function klona(x) { | ||
function klona(x) { | ||
if (typeof x !== 'object') return x; | ||
@@ -66,2 +66,12 @@ | ||
if (str === '[object DataView]') { | ||
return new x.constructor( klona(x.buffer) ); | ||
} | ||
if (str === '[object ArrayBuffer]') { | ||
return x.slice(0); | ||
} | ||
// ArrayBuffer.isView(x) | ||
// ~> `new` bcuz `Buffer.slice` => ref | ||
if (str.slice(-6) === 'Array]') { | ||
@@ -73,1 +83,3 @@ return new x.constructor(x); | ||
} | ||
exports.klona = klona; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.klona=t()}(this,(function(){return function e(t){if("object"!=typeof t)return t;var o,n,r=Object.prototype.toString.call(t);if("[object Object]"===r){if(t.constructor!==Object&&"function"==typeof t.constructor)for(o in n=new t.constructor,t)n.hasOwnProperty(o)&&n[o]!==t[o]&&(n[o]=e(t[o]));else for(o in n={},t)"__proto__"===o?Object.defineProperty(n,o,{value:e(t[o]),configurable:!0,enumerable:!0,writable:!0}):n[o]=e(t[o]);return n}if("[object Array]"===r){for(o=t.length,n=Array(o);o--;)n[o]=e(t[o]);return n}return"[object Set]"===r?(n=new Set,t.forEach((function(t){n.add(e(t))})),n):"[object Map]"===r?(n=new Map,t.forEach((function(t,o){n.set(e(o),e(t))})),n):"[object Date]"===r?new Date(+t):"[object RegExp]"===r?((n=new RegExp(t.source,t.flags)).lastIndex=t.lastIndex,n):"Array]"===r.slice(-6)?new t.constructor(t):t}})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.klona={})}(this,(function(e){e.klona=function e(t){if("object"!=typeof t)return t;var o,r,n=Object.prototype.toString.call(t);if("[object Object]"===n){if(t.constructor!==Object&&"function"==typeof t.constructor)for(o in r=new t.constructor,t)r.hasOwnProperty(o)&&r[o]!==t[o]&&(r[o]=e(t[o]));else for(o in r={},t)"__proto__"===o?Object.defineProperty(r,o,{value:e(t[o]),configurable:!0,enumerable:!0,writable:!0}):r[o]=e(t[o]);return r}if("[object Array]"===n){for(o=t.length,r=Array(o);o--;)r[o]=e(t[o]);return r}return"[object Set]"===n?(r=new Set,t.forEach((function(t){r.add(e(t))})),r):"[object Map]"===n?(r=new Map,t.forEach((function(t,o){r.set(e(o),e(t))})),r):"[object Date]"===n?new Date(+t):"[object RegExp]"===n?((r=new RegExp(t.source,t.flags)).lastIndex=t.lastIndex,r):"[object DataView]"===n?new t.constructor(e(t.buffer)):"[object ArrayBuffer]"===n?t.slice(0):"Array]"===n.slice(-6)?new t.constructor(t):t}})); |
@@ -1,4 +0,1 @@ | ||
declare module 'klona' { | ||
function klona<T>(val: T): T; | ||
export = klona; | ||
} | ||
export function klona<T>(input: T): T; |
{ | ||
"name": "klona", | ||
"version": "1.1.2", | ||
"version": "2.0.0", | ||
"repository": "lukeed/klona", | ||
"description": "A tiny (423B) and fast utility to \"deep clone\" Objects, Arrays, Dates, RegExps, and more!", | ||
"description": "A tiny (240B to 507B) and fast utility to \"deep clone\" Objects, Arrays, Dates, RegExps, and more!", | ||
"module": "dist/index.mjs", | ||
@@ -18,4 +18,13 @@ "unpkg": "dist/index.min.js", | ||
"*.d.ts", | ||
"dist" | ||
"dist", | ||
"full", | ||
"json", | ||
"lite" | ||
], | ||
"modes": { | ||
"json": "src/json.js", | ||
"lite": "src/lite.js", | ||
"default": "src/index.js", | ||
"full": "src/full.js" | ||
}, | ||
"engines": { | ||
@@ -27,3 +36,3 @@ "node": ">= 8" | ||
"pretest": "npm run build", | ||
"test": "uvu -r esm test" | ||
"test": "uvu -r esm test -i suites" | ||
}, | ||
@@ -41,4 +50,4 @@ "keywords": [ | ||
"esm": "3.2.25", | ||
"uvu": "0.0.17" | ||
"uvu": "0.3.3" | ||
} | ||
} |
140
readme.md
@@ -20,3 +20,3 @@ <div align="center"> | ||
<div align="center">A tiny (423B) and fast utility to "deep clone" Objects, Arrays, Dates, RegExps, and more!</div> | ||
<div align="center">A tiny (240B to 507B) and fast utility to "deep clone" Objects, Arrays, Dates, RegExps, and more!</div> | ||
@@ -29,12 +29,6 @@ | ||
* Safely handles complex data types<br> | ||
_Array, Date, Map, Object, RegExp, Set, TypedArray_ | ||
_Array, Date, Map, Object, RegExp, Set, TypedArray, and more_ | ||
Unlike a "shallow copy" (eg, `Object.assign`), a "deep clone" recursively traverses a source input and copies its _values_ — instead of _references_ to its values — into a new instance of that input. The result is a structurally equivalent clone that operates independently of the original source and controls its own values. | ||
Additionally, this module is delivered as: | ||
* **ES Module**: [`dist/index.mjs`](https://unpkg.com/klona/dist/index.mjs) | ||
* **CommonJS**: [`dist/index.js`](https://unpkg.com/klona/dist/index.js) | ||
* **UMD**: [`dist/klona.min.js`](https://unpkg.com/klona) | ||
> **Why "klona"?** It's "clone" in Swedish.<br> | ||
@@ -51,6 +45,47 @@ > **What's with the sheep?** [Dolly](https://en.wikipedia.org/wiki/Dolly_(sheep)). | ||
## Modes | ||
There are multiple "versions" of `klona` available, which allows you to bring only the functionality you need! | ||
#### `klona/json` | ||
> **Size (gzip):** 240 bytes<br> | ||
> **Availability:** [CommonJS](https://unpkg.com/klona/json/index.js), [ES Module](https://unpkg.com/klona/json/index.mjs), [UMD](https://unpkg.com/klona/json/index.min.js)<br> | ||
> **Ability:** JSON data types | ||
```js | ||
import { klona } from 'klona/json'; | ||
``` | ||
#### `klona/lite` | ||
> **Size (gzip):** 354 bytes<br> | ||
> **Availability:** [CommonJS](https://unpkg.com/klona/lite/index.js), [ES Module](https://unpkg.com/klona/lite/index.mjs), [UMD](https://unpkg.com/klona/lite/index.min.js)<br> | ||
> **Ability:** extends `klona/json` with support for custom class, Date, and RegExp | ||
```js | ||
import { klona } from 'klona/lite'; | ||
``` | ||
#### `klona` | ||
> **Size (gzip):** 451 bytes<br> | ||
> **Availability:** [CommonJS](https://unpkg.com/klona/dist/index.js), [ES Module](https://unpkg.com/klona/dist/index.mjs), [UMD](https://unpkg.com/klona/dist/index.min.js)<br> | ||
> **Ability:** extends `klona/lite` with support for Map, Set, DataView, ArrayBuffer, TypedArray | ||
```js | ||
import { klona } from 'klona'; | ||
``` | ||
#### `klona/full` | ||
> **Size (gzip):** 507 bytes<br> | ||
> **Availability:** [CommonJS](https://unpkg.com/klona/full/index.js), [ES Module](https://unpkg.com/klona/full/index.mjs), [UMD](https://unpkg.com/klona/full/index.min.js)<br> | ||
> **Ability:** extends `klona` with support for Symbol properties and and non-enumerable properties | ||
```js | ||
import { klona } from 'klona/full'; | ||
``` | ||
## Usage | ||
```js | ||
import klona from 'klona'; | ||
import { klona } from 'klona'; | ||
@@ -102,30 +137,69 @@ const input = { | ||
> via Node.js v10.13.0 | ||
> Running Node v12.18.3 | ||
The benchmarks can be found in the [`/bench`](/bench) directory. They are separated into multiple categories: | ||
* `JSON` – compares an array of objects comprised of JSON data types (`String`, `Number`, `null`, `Array`, `Object`) | ||
* `LITE` – like `JSON`, but adds `RegExp`, `Date` and `undefined` values | ||
* `DEFAULT` – object with `RegExp`, `Date`, `Array`, `Map`, `Set`, custom class, `Int8Array`, `DataView`, `Buffer` values | ||
* `FULL` – like `DEFAULT`, but adds `Symbol` and non-enumerable properties | ||
> **Important:** Only candidates that pass validation step(s) are listed. <br>However, `lodash` and `clone` are kept to highlight important differences. | ||
> **Note:** The `clone/include` candidate refers to its [`includeNonEnumerable` option](https://www.npmjs.com/package/clone#api) enabled. | ||
``` | ||
Load times: | ||
fast-clone 0.884ms | ||
lodash/clonedeep 27.716ms | ||
rfdc 0.782ms | ||
clone-deep 4.023ms | ||
deep-copy 0.513ms | ||
klona 0.333ms | ||
lodash/clonedeep 29.257ms | ||
rfdc 0.511ms | ||
clone 0.576ms | ||
clone-deep 2.494ms | ||
deep-copy 0.451ms | ||
klona/full 0.408ms | ||
klona 0.265ms | ||
klona/lite 0.308ms | ||
klona/json 0.263ms | ||
Validation: | ||
✘ JSON.stringify (FAILED @ "initial copy") | ||
✘ fast-clone (FAILED @ "initial copy") | ||
✔ lodash | ||
✘ rfdc (FAILED @ "initial copy") | ||
✔ clone-deep | ||
✘ deep-copy (FAILED @ "initial copy") | ||
✔ klona | ||
Benchmark :: JSON | ||
JSON.stringify x 50,156 ops/sec ±0.32% (93 runs sampled) | ||
lodash x 44,269 ops/sec ±0.48% (94 runs sampled) | ||
rfdc x 202,428 ops/sec ±0.91% (94 runs sampled) | ||
clone x 38,947 ops/sec ±0.34% (97 runs sampled) | ||
clone/include x 25,021 ops/sec ±0.22% (93 runs sampled) | ||
clone-deep x 98,676 ops/sec ±0.20% (93 runs sampled) | ||
deep-copy x 129,432 ops/sec ±0.25% (98 runs sampled) | ||
klona/full x 52,482 ops/sec ±0.26% (98 runs sampled) | ||
klona x 257,905 ops/sec ±0.54% (97 runs sampled) | ||
klona/lite x 301,324 ops/sec ±0.31% (97 runs sampled) | ||
klona/json x 336,300 ops/sec ±0.17% (96 runs sampled) | ||
Benchmark: | ||
JSON.stringify x 36,628 ops/sec ±1.34% (89 runs sampled) | ||
fast-clone x 23,518 ops/sec ±1.18% (91 runs sampled) | ||
lodash x 33,810 ops/sec ±1.34% (94 runs sampled) | ||
rfdc x 181,634 ops/sec ±0.71% (95 runs sampled) | ||
clone-deep x 84,558 ops/sec ±0.19% (96 runs sampled) | ||
deep-copy x 112,866 ops/sec ±1.26% (94 runs sampled) | ||
klona x 220,356 ops/sec ±0.34% (97 runs sampled) | ||
Benchmark :: LITE | ||
lodash x 35,046 ops/sec ±0.20% (96 runs sampled) | ||
clone x 35,425 ops/sec ±0.46% (93 runs sampled) | ||
clone/include x 22,296 ops/sec ±0.31% (95 runs sampled) | ||
clone-deep x 85,550 ops/sec ±0.25% (97 runs sampled) | ||
klona/full x 46,303 ops/sec ±0.30% (96 runs sampled) | ||
klona x 211,161 ops/sec ±0.19% (99 runs sampled) | ||
klona/lite x 241,172 ops/sec ±0.17% (97 runs sampled) | ||
Benchmark :: DEFAULT | ||
lodash x 48,006 ops/sec ±0.34% (95 runs sampled) | ||
✘ Buffer | ||
✘ Map keys | ||
clone x 91,191 ops/sec ±0.24% (94 runs sampled) | ||
✘ DataView | ||
clone/include x 59,209 ops/sec ±0.28% (96 runs sampled) | ||
✘ DataView | ||
klona/full x 84,333 ops/sec ±0.27% (95 runs sampled) | ||
klona x 208,685 ops/sec ±0.23% (95 runs sampled) | ||
Benchmark :: FULL | ||
lodash x 51,634 ops/sec ±0.44% (94 runs sampled) | ||
✘ Buffer | ||
✘ Map keys | ||
✘ Missing non-enumerable Properties | ||
clone/include x 44,020 ops/sec ±0.31% (93 runs sampled) | ||
✘ DataView | ||
✘ Incorrect non-enumerable Properties | ||
klona/full x 78,217 ops/sec ±0.61% (97 runs sampled) | ||
``` | ||
@@ -138,3 +212,3 @@ | ||
* [dset](https://github.com/lukeed/dset) – safely **write** into deep properties in 160 bytes | ||
* [dequal](https://github.com/lukeed/dequal) – safely check for deep equality in 247 bytes | ||
* [dequal](https://github.com/lukeed/dequal) – safely check for deep equality in 304 to 489 bytes | ||
@@ -141,0 +215,0 @@ |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
22573
16
375
214