-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- handles symbols, accessors, non-enumerables - add initial `Symbol` tests - 507B -- 52k op/s
- Loading branch information
Showing
6 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ node_modules | |
/dist | ||
/json | ||
/lite | ||
/full |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
function set(obj, key, val) { | ||
if (typeof val.value === 'object') val.value = klona(val.value); | ||
if (!val.enumerable || val.get || val.set || !val.configurable || !val.writable || key === '__proto__') { | ||
Object.defineProperty(obj, key, val); | ||
} else obj[key] = val.value; | ||
} | ||
|
||
export function klona(x) { | ||
if (typeof x !== 'object') return x; | ||
|
||
var i=0, k, list, tmp, str=Object.prototype.toString.call(x); | ||
|
||
if (str === '[object Object]') { | ||
tmp = typeof x.constructor === 'function' ? new x.constructor() : Object.create(null); | ||
} else if (str === '[object Array]') { | ||
tmp = Array(x.length); | ||
} else if (str === '[object Set]') { | ||
tmp = new Set; | ||
x.forEach(function (val) { | ||
tmp.add(klona(val)); | ||
}); | ||
} else if (str === '[object Map]') { | ||
tmp = new Map; | ||
x.forEach(function (val, key) { | ||
tmp.set(klona(key), klona(val)); | ||
}); | ||
} else if (str === '[object Date]') { | ||
tmp = new Date(+x); | ||
} else if (str === '[object RegExp]') { | ||
tmp = new RegExp(x.source, x.flags); | ||
} else if (str === '[object DataView]') { | ||
tmp = new x.constructor( klona(x.buffer) ); | ||
} else if (str === '[object ArrayBuffer]') { | ||
tmp = x.slice(0); | ||
} else if (str.slice(-6) === 'Array]') { | ||
// ArrayBuffer.isView(x) | ||
// ~> `new` bcuz `Buffer.slice` => ref | ||
tmp = new x.constructor(x); | ||
} | ||
|
||
if (tmp) { | ||
for (list = Object.getOwnPropertySymbols(x); i < list.length; i++) { | ||
set(tmp, list[i], Object.getOwnPropertyDescriptor(x, list[i])); | ||
} | ||
|
||
for (i=0, list=Object.getOwnPropertyNames(x); i < list.length; i++) { | ||
if (Object.hasOwnProperty.call(tmp, k=list[i]) && tmp[k] === x[k]) continue; | ||
set(tmp, k, Object.getOwnPropertyDescriptor(x, k)); | ||
} | ||
} | ||
|
||
return tmp || x; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as suites from './suites'; | ||
import { klona } from '../src/full'; | ||
|
||
suites.API(klona); | ||
|
||
suites.Strings(klona); | ||
suites.Booleans(klona); | ||
suites.Numbers(klona); | ||
suites.Nully(klona); | ||
|
||
suites.Dates(klona); | ||
suites.RegExps(klona); | ||
|
||
suites.Objects(klona); | ||
suites.Arrays(klona); | ||
|
||
suites.Functions(klona); | ||
suites.Pollutions(klona); | ||
suites.Classes(klona); | ||
|
||
suites.Maps(klona); | ||
suites.Sets(klona); | ||
|
||
suites.TypedArrays(klona); | ||
|
||
suites.Symbols(klona); | ||
// suites.Descriptors(klona); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { suite } from 'uvu'; | ||
import * as assert from 'assert'; | ||
|
||
export default function (klona) { | ||
const Symbols = suite('Symbol'); | ||
|
||
Symbols('direct', () => { | ||
const input = Symbol('input'); | ||
const output = klona(input); | ||
|
||
assert.deepStrictEqual(input, output); | ||
}); | ||
|
||
// https://github.com/lukeed/klona/issues/16 | ||
Symbols('object :: enumerable key', () => { | ||
const key = Symbol('key'); | ||
const input = { foo: 123, [key]: 456 }; | ||
console.log('START'); | ||
const output = klona(input); | ||
console.log('~> output', output); | ||
|
||
assert.equal(output[key], 456); | ||
assert.deepStrictEqual(input, output); | ||
}); | ||
|
||
Symbols.run(); | ||
} |