-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
compositeKey
method from richer keys proposal
- Loading branch information
Showing
12 changed files
with
176 additions
and
0 deletions.
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 |
---|---|---|
|
@@ -470,6 +470,7 @@ module.exports = { | |
}, | ||
globals: { | ||
asap: true, | ||
compositeKey: true, | ||
Observable: true, | ||
}, | ||
}, | ||
|
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
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,3 @@ | ||
require('../modules/esnext.composite-key'); | ||
|
||
module.exports = require('../internals/path').compositeKey; |
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,44 @@ | ||
var Map = require('../modules/es.map'); | ||
var WeakMap = require('../modules/es.weak-map'); | ||
var create = require('../internals/object-create'); | ||
var isObject = require('../internals/is-object'); | ||
var $Object = require('../internals/path').Object; | ||
var freeze = $Object && $Object.freeze; | ||
|
||
var Node = function () { | ||
this.value = null; | ||
this.primitives = null; | ||
this.objectsByIndex = create(null); | ||
}; | ||
|
||
Node.prototype.get = function () { | ||
return this.value || (this.value = freeze ? freeze(create(null)) : create(null)); | ||
}; | ||
|
||
Node.prototype.next = function (i, it, IS_OBJECT) { | ||
var store = IS_OBJECT | ||
? this.objectsByIndex[i] || (this.objectsByIndex[i] = new WeakMap()) | ||
: this.primitives || (this.primitives = new Map()); | ||
var entry = store.get(it); | ||
if (!entry) store.set(it, entry = new Node()); | ||
return entry; | ||
}; | ||
|
||
var root = new Node(); | ||
|
||
require('../internals/export')({ global: true }, { | ||
compositeKey: function compositeKey() { | ||
var active = root; | ||
var length = arguments.length; | ||
var i, it; | ||
// for prevent leaking, start from objects | ||
for (i = 0; i < length; i++) { | ||
if (isObject(it = arguments[i])) active = active.next(i, it, true); | ||
} | ||
if (active === root) throw TypeError('Composite keys must contain a non-primitive component'); | ||
for (i = 0; i < length; i++) { | ||
if (!isObject(it = arguments[i])) active = active.next(i, it, false); | ||
} | ||
return active.get(); | ||
} | ||
}); |
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
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,46 @@ | ||
/* eslint-disable no-self-compare */ | ||
import { FREEZING } from '../helpers/constants'; | ||
|
||
import compositeKey from 'core-js-pure/features/composite-key'; | ||
import { getPrototypeOf, isFrozen } from 'core-js-pure/features/object'; | ||
|
||
QUnit.test('compositeKey', assert => { | ||
assert.isFunction(compositeKey); | ||
if (compositeKey.name) assert.name(compositeKey, 'compositeKey'); | ||
|
||
const key = compositeKey({}); | ||
assert.same(typeof key, 'object'); | ||
assert.same({}.toString.call(key), '[object Object]'); | ||
assert.same(getPrototypeOf(key), null); | ||
if (FREEZING) assert.ok(isFrozen(key)); | ||
|
||
const a = ['a']; | ||
const b = ['b']; | ||
const c = ['c']; | ||
|
||
assert.ok(compositeKey(a) === compositeKey(a)); | ||
assert.ok(compositeKey(a) !== compositeKey(['a'])); | ||
assert.ok(compositeKey(a) !== compositeKey(a, 1)); | ||
assert.ok(compositeKey(a) !== compositeKey(a, b)); | ||
assert.ok(compositeKey(a, 1) === compositeKey(a, 1)); | ||
assert.ok(compositeKey(a, b) === compositeKey(a, b)); | ||
assert.ok(compositeKey(a, b) !== compositeKey(b, a)); | ||
assert.ok(compositeKey(a, b, c) === compositeKey(a, b, c)); | ||
assert.ok(compositeKey(a, b, c) !== compositeKey(c, b, a)); | ||
assert.ok(compositeKey(a, b, c) !== compositeKey(a, c, b)); | ||
assert.ok(compositeKey(a, b, c, 1) !== compositeKey(a, b, c)); | ||
assert.ok(compositeKey(a, b, c, 1) === compositeKey(a, b, c, 1)); | ||
assert.ok(compositeKey(1, a) === compositeKey(1, a)); | ||
assert.ok(compositeKey(1, a) !== compositeKey(a, 1)); | ||
assert.ok(compositeKey(1, a, 2, b) === compositeKey(1, a, 2, b)); | ||
assert.ok(compositeKey(1, a, 2, b) !== compositeKey(1, a, b, 2)); | ||
assert.ok(compositeKey(1, 2, a, b) === compositeKey(1, 2, a, b)); | ||
assert.ok(compositeKey(1, 2, a, b) !== compositeKey(1, a, b, 2)); | ||
assert.ok(compositeKey(a, a) === compositeKey(a, a)); | ||
assert.ok(compositeKey(a, a) !== compositeKey(a, ['a'])); | ||
assert.ok(compositeKey(a, a) !== compositeKey(a, b)); | ||
|
||
assert.throws(() => compositeKey(), TypeError); | ||
assert.throws(() => compositeKey(1, 2), TypeError); | ||
assert.throws(() => compositeKey('foo', null, true), TypeError); | ||
}); |
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,46 @@ | ||
/* eslint-disable no-self-compare */ | ||
import { FREEZING } from '../helpers/constants'; | ||
|
||
const { getPrototypeOf, isFrozen } = Object; | ||
|
||
QUnit.test('compositeKey', assert => { | ||
assert.isFunction(compositeKey); | ||
assert.name(compositeKey, 'compositeKey'); | ||
assert.looksNative(compositeKey); | ||
|
||
const key = compositeKey({}); | ||
assert.same(typeof key, 'object'); | ||
assert.same({}.toString.call(key), '[object Object]'); | ||
assert.same(getPrototypeOf(key), null); | ||
if (FREEZING) assert.ok(isFrozen(key)); | ||
|
||
const a = ['a']; | ||
const b = ['b']; | ||
const c = ['c']; | ||
|
||
assert.ok(compositeKey(a) === compositeKey(a)); | ||
assert.ok(compositeKey(a) !== compositeKey(['a'])); | ||
assert.ok(compositeKey(a) !== compositeKey(a, 1)); | ||
assert.ok(compositeKey(a) !== compositeKey(a, b)); | ||
assert.ok(compositeKey(a, 1) === compositeKey(a, 1)); | ||
assert.ok(compositeKey(a, b) === compositeKey(a, b)); | ||
assert.ok(compositeKey(a, b) !== compositeKey(b, a)); | ||
assert.ok(compositeKey(a, b, c) === compositeKey(a, b, c)); | ||
assert.ok(compositeKey(a, b, c) !== compositeKey(c, b, a)); | ||
assert.ok(compositeKey(a, b, c) !== compositeKey(a, c, b)); | ||
assert.ok(compositeKey(a, b, c, 1) !== compositeKey(a, b, c)); | ||
assert.ok(compositeKey(a, b, c, 1) === compositeKey(a, b, c, 1)); | ||
assert.ok(compositeKey(1, a) === compositeKey(1, a)); | ||
assert.ok(compositeKey(1, a) !== compositeKey(a, 1)); | ||
assert.ok(compositeKey(1, a, 2, b) === compositeKey(1, a, 2, b)); | ||
assert.ok(compositeKey(1, a, 2, b) !== compositeKey(1, a, b, 2)); | ||
assert.ok(compositeKey(1, 2, a, b) === compositeKey(1, 2, a, b)); | ||
assert.ok(compositeKey(1, 2, a, b) !== compositeKey(1, a, b, 2)); | ||
assert.ok(compositeKey(a, a) === compositeKey(a, a)); | ||
assert.ok(compositeKey(a, a) !== compositeKey(a, ['a'])); | ||
assert.ok(compositeKey(a, a) !== compositeKey(a, b)); | ||
|
||
assert.throws(() => compositeKey(), TypeError); | ||
assert.throws(() => compositeKey(1, 2), TypeError); | ||
assert.throws(() => compositeKey('foo', null, true), TypeError); | ||
}); |
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