@teamawesome/access
Advanced tools
Comparing version 1.0.13 to 1.0.15
@@ -0,0 +0,0 @@ const suite = (new require('benchmark').Suite)(); |
@@ -0,0 +0,0 @@ const suite = (new require('benchmark').Suite)(); |
@@ -5,2 +5,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); | ||
@@ -47,2 +52,5 @@ | ||
_access.default.register(undefined, proxy); | ||
_access.default.register(undefined, proxy); | ||
var _default = proxy; | ||
exports.default = _default; |
@@ -8,6 +8,10 @@ "use strict"; | ||
}); | ||
exports.types = exports.wrap = exports.access = exports.default = void 0; | ||
exports.wrap = exports.types = exports.default = exports.access = void 0; | ||
require("./handlers/array"); | ||
require("./handlers/dom-string-map"); | ||
require("./handlers/multi-dict"); | ||
require("./handlers/object"); | ||
@@ -14,0 +18,0 @@ |
{ | ||
"name": "@teamawesome/access", | ||
"version": "1.0.13", | ||
"version": "1.0.15", | ||
"description": "Provide a unified interface for objects.", | ||
@@ -18,3 +18,3 @@ "keywords": [ | ||
"lint": "eslint src/ --fix", | ||
"test": "mocha --require ./test/setup.js", | ||
"test": "mocha", | ||
"benchmark:object": "node bench/object.js", | ||
@@ -28,20 +28,22 @@ "benchmark:map": "node bench/map.js", | ||
"devDependencies": { | ||
"@babel/cli": "^7.14.5", | ||
"@babel/core": "^7.14.6", | ||
"@babel/eslint-parser": "^7.14.5", | ||
"@babel/plugin-transform-runtime": "^7.14.5", | ||
"@babel/preset-env": "^7.14.5", | ||
"@babel/cli": "^7.15.7", | ||
"@babel/core": "^7.15.8", | ||
"@babel/eslint-parser": "^7.15.8", | ||
"@babel/plugin-transform-runtime": "^7.15.8", | ||
"@babel/preset-env": "^7.15.8", | ||
"@teamawesome/multi-dict": "^2.0.2", | ||
"benchmark": "^2.1.4", | ||
"dom-storage": "^2.1.0", | ||
"eslint": "^7.29.0", | ||
"eslint": "^7.32.0", | ||
"eslint-config-airbnb-base": "^14.2.1", | ||
"eslint-plugin-import": "^2.23.4", | ||
"mocha": "^9.0.1", | ||
"eslint-plugin-import": "^2.25.2", | ||
"jsdom": "^18.0.0", | ||
"mocha": "^9.1.3", | ||
"should": "^13.2.3", | ||
"should-sinon": "0.0.6", | ||
"sinon": "^11.1.1" | ||
"sinon": "^11.1.2" | ||
}, | ||
"dependencies": { | ||
"@babel/runtime": "^7.14.6" | ||
"@babel/runtime": "^7.15.4" | ||
} | ||
} |
@@ -7,2 +7,4 @@ # Access | ||
* Storage (localStorage, sessionStorage) | ||
* [@teamawesome/multi-dict](https://www.npmjs.com/package/@teamawesome/multi-dict) | ||
* [DOMStringMap](https://developer.mozilla.org/en-US/docs/Web/API/DOMStringMap) (dataset) | ||
* Map | ||
@@ -9,0 +11,0 @@ * WeakMap |
@@ -41,1 +41,3 @@ import access from '../access'; | ||
access.register(undefined, proxy); | ||
export default proxy; |
import './handlers/array'; | ||
import './handlers/dom-string-map'; | ||
import './handlers/multi-dict'; | ||
import './handlers/object'; | ||
@@ -3,0 +5,0 @@ import './handlers/storage'; |
const should = require('should'); | ||
const sinon = require('sinon'); | ||
require('should-sinon'); | ||
const {access, types} = require('../dist'); | ||
const { access, types } = require('../dist'); | ||
it('unregistered type', () => { | ||
const custom = new class { | ||
const custom = new class { | ||
get = sinon.stub().returns(1); | ||
@@ -15,99 +15,98 @@ | ||
clear = sinon.stub().returns(undefined); | ||
}(); | ||
}(); | ||
custom.set = sinon.stub().returns(custom); | ||
custom.set = sinon.stub().returns(custom); | ||
access.get(custom, 'a').should.equal(1); | ||
custom.get.should.be.calledOnce() | ||
.and.be.calledWith('a'); | ||
access.get(custom, 'a').should.equal(1); | ||
custom.get.should.be.calledOnce() | ||
.and.be.calledWith('a'); | ||
access.set(custom, 'a', 1).should.equal(custom); | ||
custom.set.should.be.calledOnce() | ||
.and.be.calledWith('a', 1); | ||
access.set(custom, 'a', 1).should.equal(custom); | ||
custom.set.should.be.calledOnce() | ||
.and.be.calledWith('a', 1); | ||
access.has(custom, 'a').should.be.true(); | ||
custom.has.should.be.calledOnce() | ||
.and.be.calledWith('a'); | ||
access.has(custom, 'a').should.be.true(); | ||
custom.has.should.be.calledOnce() | ||
.and.be.calledWith('a'); | ||
access.delete(custom, 'a').should.be.true(); | ||
custom.delete.should.be.calledOnce() | ||
.and.be.calledWith('a'); | ||
access.delete(custom, 'a').should.be.true(); | ||
custom.delete.should.be.calledOnce() | ||
.and.be.calledWith('a'); | ||
should(access.clear(custom)).be.undefined(); | ||
custom.clear.should.be.calledOnce(); | ||
should(access.clear(custom)).be.undefined(); | ||
custom.clear.should.be.calledOnce(); | ||
(() => { | ||
access.size(custom) | ||
}).should.throw(TypeError) | ||
(() => { | ||
access.size(custom); | ||
}).should.throw(TypeError); | ||
}); | ||
it('register & unregister', () => { | ||
class Custom { | ||
} | ||
class Custom { | ||
} | ||
const proxy = {}; | ||
const proxy = {}; | ||
access.register(Custom, proxy); | ||
types.should.have.value(Custom, proxy); | ||
access.register(Custom, proxy); | ||
types.should.have.value(Custom, proxy); | ||
access.unregister(Custom); | ||
types.should.not.have.value(Custom, proxy); | ||
access.unregister(Custom); | ||
types.should.not.have.value(Custom, proxy); | ||
}); | ||
it('registered mixed type', () => { | ||
class Custom { | ||
class Custom { | ||
get = sinon.stub().returns(1) | ||
} | ||
} | ||
const custom = new Custom(); | ||
const custom = new Custom(); | ||
const proxy = { | ||
// No getter in proxy. | ||
set: sinon.stub().returns(custom), | ||
has: sinon.stub().returns(true), | ||
delete: sinon.stub().returns(true), | ||
clear: sinon.stub().returns(undefined), | ||
}; | ||
const proxy = { | ||
// No getter in proxy. | ||
set: sinon.stub().returns(custom), | ||
has: sinon.stub().returns(true), | ||
delete: sinon.stub().returns(true), | ||
clear: sinon.stub().returns(undefined), | ||
}; | ||
access.register(Custom, proxy); | ||
access.register(Custom, proxy); | ||
access.get(custom, 'a').should.equal(1); | ||
custom.get.should.be.calledOnce() | ||
.and.be.calledWith('a'); | ||
access.get(custom, 'a').should.equal(1); | ||
custom.get.should.be.calledOnce() | ||
.and.be.calledWith('a'); | ||
access.set(custom, 'a', 1).should.equal(custom); | ||
proxy.set.should.be.calledOnce() | ||
.and.be.calledWith(custom, 'a', 1); | ||
access.set(custom, 'a', 1).should.equal(custom); | ||
proxy.set.should.be.calledOnce() | ||
.and.be.calledWith(custom, 'a', 1); | ||
access.has(custom, 'a').should.be.true(); | ||
proxy.has.should.be.calledOnce() | ||
.and.be.calledWith(custom, 'a'); | ||
access.has(custom, 'a').should.be.true(); | ||
proxy.has.should.be.calledOnce() | ||
.and.be.calledWith(custom, 'a'); | ||
access.delete(custom, 'a').should.be.true(); | ||
proxy.delete.should.be.calledOnce() | ||
.and.be.calledWith(custom, 'a'); | ||
access.delete(custom, 'a').should.be.true(); | ||
proxy.delete.should.be.calledOnce() | ||
.and.be.calledWith(custom, 'a'); | ||
should(access.clear(custom)).be.undefined(); | ||
proxy.delete.should.be.calledOnce() | ||
.and.be.calledWith(custom); | ||
should(access.clear(custom)).be.undefined(); | ||
proxy.delete.should.be.calledOnce() | ||
.and.be.calledWith(custom); | ||
}); | ||
it('proxy precedence', () => { | ||
class Custom { | ||
class Custom { | ||
get = sinon.stub().returns(1) | ||
} | ||
} | ||
const custom = new Custom(); | ||
const custom = new Custom(); | ||
const proxy = { | ||
get: sinon.stub().returns(1), | ||
}; | ||
const proxy = { | ||
get: sinon.stub().returns(1), | ||
}; | ||
access.register(Custom, proxy); | ||
access.get(custom, 'a'); | ||
access.register(Custom, proxy); | ||
access.get(custom, 'a'); | ||
custom.get.should.not.be.called(); | ||
proxy.get.should.be.calledOnce() | ||
.and.be.calledWith(custom, 'a'); | ||
}); | ||
custom.get.should.not.be.called(); | ||
proxy.get.should.be.calledOnce() | ||
.and.be.calledWith(custom, 'a'); | ||
}); |
@@ -1,66 +0,71 @@ | ||
const {access} = require('../dist'); | ||
const should = require('should'); | ||
const { access } = require('../dist'); | ||
const types = require('./types'); | ||
types.forEach(({name, key1, key2, factory}) => { | ||
describe(name, () => { | ||
let obj; | ||
types.forEach(({ | ||
setup, | ||
name, | ||
key1, | ||
key2, | ||
factory, | ||
}) => { | ||
describe(name, () => { | ||
let obj; | ||
beforeEach(() => { | ||
obj = factory(); | ||
}); | ||
beforeEach(() => { | ||
if (setup) { | ||
setup(); | ||
} | ||
it('get', () => { | ||
access.get(obj, key1).should.equal('value'); | ||
should(access.get(obj, 'does_not_exist')).be.undefined(); | ||
}); | ||
obj = factory(); | ||
}); | ||
it('set', () => { | ||
access.set(obj, key2, 'new_value'); | ||
access.get(obj, key2).should.equal('new_value'); | ||
it('get', () => { | ||
access.get(obj, key1).should.equal('value'); | ||
should(access.get(obj, 'does_not_exist')).be.undefined(); | ||
}); | ||
should(obj).have.value(key2, 'new_value'); | ||
should(obj).have.size(2); | ||
}); | ||
it('set', () => { | ||
access.set(obj, key2, 'new_value'); | ||
access.get(obj, key2).should.equal('new_value'); | ||
}); | ||
it('has', () => { | ||
access.has(obj, key1).should.be.true(); | ||
access.has(obj, 'does_not_exist').should.be.false(); | ||
}); | ||
it('has', () => { | ||
access.has(obj, key1).should.be.true(); | ||
access.has(obj, 'does_not_exist').should.be.false(); | ||
}); | ||
it('delete', () => { | ||
access.delete(obj, key1).should.be.true(); | ||
access.delete(obj, 'does_not_exist').should.be.false(); | ||
access.has(obj, key1).should.be.false(); | ||
it('delete', () => { | ||
access.delete(obj, key1).should.be.true(); | ||
access.delete(obj, 'does_not_exist').should.be.false(); | ||
access.has(obj, key1).should.be.false(); | ||
}); | ||
should(obj).not.have.key(key1); | ||
}); | ||
it('clear', () => { | ||
should(access.clear(obj)).be.undefined(); | ||
it('clear', () => { | ||
should(access.clear(obj)).be.undefined(); | ||
access.size(obj).should.equal(0); | ||
}); | ||
should(obj).be.empty(); | ||
}); | ||
it('size', () => { | ||
access.size(obj).should.equal(1); | ||
access.set(obj, key2, 'new_value'); | ||
access.size(obj).should.equal(2); | ||
}); | ||
it('size', () => { | ||
access.size(obj).should.equal(1); | ||
access.set(obj, key2, 'new_value'); | ||
access.size(obj).should.equal(2); | ||
}); | ||
it('keys', () => { | ||
access.keys(obj).should.be.iterator(); | ||
[...access.keys(obj)].should.eql([key1]); | ||
}); | ||
it('keys', () => { | ||
access.keys(obj).should.be.iterator(); | ||
[...access.keys(obj)].should.eql([key1]); | ||
}); | ||
it('values', () => { | ||
access.values(obj).should.be.iterator(); | ||
[...access.values(obj)].should.eql(['value']); | ||
}); | ||
it('values', () => { | ||
access.values(obj).should.be.iterator(); | ||
[...access.values(obj)].should.eql(['value']); | ||
}); | ||
it('entries', () => { | ||
access.entries(obj).should.be.iterator(); | ||
[...access.entries(obj)].should.containDeep([[key1, 'value']]) | ||
}); | ||
it('entries', () => { | ||
access.entries(obj).should.be.iterator(); | ||
[...access.entries(obj)].should.containDeep([[key1, 'value']]); | ||
}); | ||
}); | ||
}); | ||
}); |
const Storage = require('dom-storage'); | ||
const { default: Dictionary } = require('@teamawesome/multi-dict'); | ||
const { JSDOM } = require('jsdom'); | ||
const localStorage = typeof window === 'object' && window.Storage | ||
? window.localStorage | ||
: new Storage(null, {strict: true}); | ||
? window.localStorage | ||
: new Storage(null, { strict: true }); | ||
const dom = new JSDOM(); | ||
module.exports = [ | ||
{ | ||
name: 'Object', | ||
key1: 'key', | ||
key2: 'new_key', | ||
factory: () => ({key: 'value'}) | ||
{ | ||
name: 'Object', | ||
key1: 'key', | ||
key2: 'new_key', | ||
factory: () => ({ key: 'value' }), | ||
}, | ||
{ | ||
name: 'Object (no proto)', | ||
key1: 'key', | ||
key2: 'new_key', | ||
factory: () => Object.assign(Object.create(null), { key: 'value' }), | ||
}, | ||
{ | ||
name: 'Array', | ||
key1: 0, | ||
key2: 1, | ||
factory: () => ['value'], | ||
}, | ||
{ | ||
name: 'Map', | ||
key1: 'key', | ||
key2: {}, | ||
factory: () => new Map([['key', 'value']]), | ||
}, | ||
{ | ||
name: 'Storage', | ||
key1: 'key', | ||
key2: 'new_key', | ||
factory: () => { | ||
localStorage.clear(); | ||
localStorage.setItem('key', 'value'); | ||
return localStorage; | ||
}, | ||
{ | ||
name: 'Object (no proto)', | ||
key1: 'key', | ||
key2: 'new_key', | ||
factory: () => Object.assign(Object.create(null), {key: 'value'}) | ||
setup: () => { | ||
if (typeof window !== 'object' || !window.Storage) { | ||
global.window = global.window || {}; | ||
global.window.Storage = require('dom-storage'); | ||
delete require.cache[require.resolve('../dist/handlers/storage')]; | ||
require('../dist/handlers/storage'); | ||
} | ||
}, | ||
{ | ||
name: 'Array', | ||
key1: 0, | ||
key2: 1, | ||
factory: () => ['value'] | ||
}, | ||
{ | ||
name: '@teamawesome/multi-dict', | ||
key1: [1, 2], | ||
key2: [{}, 2], | ||
factory: () => new Dictionary([[1, 2, 'value']]), | ||
}, | ||
{ | ||
name: 'DOMStringMap', | ||
key1: 'key', | ||
key2: 'new_key', | ||
factory: () => { | ||
const { dataset } = dom.window.document.createElement('div'); | ||
dataset.key = 'value'; | ||
return dataset; | ||
}, | ||
{ | ||
name: 'Map', | ||
key1: 'key', | ||
key2: {}, | ||
factory: () => new Map([['key', 'value']]) | ||
setup: () => { | ||
if (typeof window !== 'object' || !window.DOMStringMap) { | ||
global.window = global.window || {}; | ||
global.window.DOMStringMap = dom.window.DOMStringMap; | ||
delete require.cache[require.resolve('../dist/handlers/dom-string-map')]; | ||
require('../dist/handlers/dom-string-map'); | ||
} | ||
}, | ||
{ | ||
name: 'Storage', | ||
key1: 'key', | ||
key2: 'new_key', | ||
factory: () => { | ||
localStorage.clear(); | ||
localStorage.setItem('key', 'value'); | ||
return localStorage | ||
} | ||
} | ||
]; | ||
}, | ||
]; |
@@ -1,112 +0,119 @@ | ||
const {wrap, access} = require('../dist'); | ||
const types = require('./types'); | ||
const should = require('should'); | ||
const sinon = require('sinon'); | ||
const { wrap, access } = require('../dist'); | ||
const types = require('./types'); | ||
require('should-sinon'); | ||
describe('Wrapped', () => { | ||
types.forEach(({name, key1, key2, factory}) => { | ||
describe(name, () => { | ||
let obj; | ||
let wrapped; | ||
types.forEach(({ | ||
name, key1, key2, factory, | ||
}) => { | ||
describe(name, () => { | ||
let obj; | ||
let wrapped; | ||
beforeEach(() => { | ||
obj = factory(); | ||
wrapped = wrap(obj); | ||
}); | ||
beforeEach(() => { | ||
obj = factory(); | ||
wrapped = wrap(obj); | ||
}); | ||
it('get', () => { | ||
wrapped.get(key1).should.equal('value'); | ||
should(wrapped.get('does_not_exist')).be.undefined(); | ||
}); | ||
it('get', () => { | ||
wrapped.get(key1).should.equal('value'); | ||
should(wrapped.get('does_not_exist')).be.undefined(); | ||
}); | ||
it('set', () => { | ||
wrapped.set(key2, 'new_value'); | ||
wrapped.get(key2).should.equal('new_value'); | ||
it('set', () => { | ||
wrapped.set(key2, 'new_value'); | ||
wrapped.get(key2).should.equal('new_value'); | ||
wrapped.size.should.equal(2); | ||
}); | ||
should(obj).have.value(key2, 'new_value'); | ||
should(obj).have.size(2); | ||
}); | ||
it('has', () => { | ||
wrapped.has(key1).should.be.true(); | ||
wrapped.has('does_not_exist').should.be.false(); | ||
}); | ||
it('has', () => { | ||
wrapped.has(key1).should.be.true(); | ||
wrapped.has('does_not_exist').should.be.false(); | ||
}); | ||
it('delete', () => { | ||
wrapped.delete(key1).should.be.true(); | ||
wrapped.delete('does_not_exist').should.be.false(); | ||
wrapped.has(key1).should.be.false(); | ||
}); | ||
it('delete', () => { | ||
wrapped.delete(key1).should.be.true(); | ||
wrapped.delete('does_not_exist').should.be.false(); | ||
wrapped.has(key1).should.be.false(); | ||
it('clear', () => { | ||
should(wrapped.clear()).be.undefined(); | ||
wrapped.size.should.equal(0); | ||
}); | ||
should(obj).not.have.key(key1); | ||
}); | ||
it('size', () => { | ||
wrapped.size.should.equal(1); | ||
wrapped.set(key2, 'new_value'); | ||
wrapped.size.should.equal(2); | ||
wrapped.delete(key2); | ||
wrapped.size.should.equal(1); | ||
}); | ||
it('clear', () => { | ||
should(wrapped.clear()).be.undefined(); | ||
should(obj).be.empty(); | ||
}); | ||
it('keys', () => { | ||
wrapped.keys().should.be.iterator(); | ||
[...wrapped.keys()].should.eql([key1]); | ||
}); | ||
it('size', () => { | ||
wrapped.size.should.equal(1); | ||
wrapped.set(key2, 'new_value'); | ||
wrapped.size.should.equal(2); | ||
wrapped.delete(key2); | ||
wrapped.size.should.equal(1); | ||
}); | ||
it('values', () => { | ||
wrapped.values().should.be.iterator(); | ||
[...wrapped.values()].should.eql(['value']); | ||
}); | ||
it('keys', () => { | ||
wrapped.keys().should.be.iterator(); | ||
[...wrapped.keys()].should.eql([key1]); | ||
}); | ||
it('values', () => { | ||
wrapped.values().should.be.iterator(); | ||
[...wrapped.values()].should.eql(['value']); | ||
}); | ||
it('entries', () => { | ||
wrapped.entries().should.be.iterator(); | ||
[...wrapped.entries()].should.containDeep([[key1, 'value']]) | ||
}); | ||
}); | ||
it('entries', () => { | ||
wrapped.entries().should.be.iterator(); | ||
[...wrapped.entries()].should.containDeep([[key1, 'value']]); | ||
}); | ||
}); | ||
}); | ||
it('missing handlers', () => { | ||
(() => { | ||
wrap(class { | ||
}); | ||
}).should.throw(TypeError); | ||
}); | ||
it('missing handlers', () => { | ||
(() => { | ||
wrap(class { | ||
}); | ||
}).should.throw(TypeError); | ||
}); | ||
it('proxy precedence', () => { | ||
const proxy = { | ||
get: sinon.stub().returns(1), | ||
}; | ||
const empty = () => { | ||
}; | ||
it('proxy precedence', () => { | ||
const proxy = { | ||
get: sinon.stub().returns(1), | ||
}; | ||
const empty = () => { | ||
}; | ||
class Custom { | ||
class Custom { | ||
get = sinon.stub(); | ||
set = empty; | ||
has = empty; | ||
delete = empty; | ||
clear = empty; | ||
size = 1; | ||
keys = empty; | ||
values = empty; | ||
entries = empty; | ||
[Symbol.iterator] = empty; | ||
} | ||
} | ||
access.register(Custom, proxy); | ||
access.register(Custom, proxy); | ||
const custom = new Custom(); | ||
const wrapped = wrap(custom); | ||
const custom = new Custom(); | ||
const wrapped = wrap(custom); | ||
wrapped.get('key'); | ||
wrapped.get('key'); | ||
custom.get.should.not.be.called(); | ||
proxy.get.should.be.calledOnce() | ||
.and.be.calledWith(custom, 'key'); | ||
}); | ||
}); | ||
custom.get.should.not.be.called(); | ||
proxy.get.should.be.calledOnce() | ||
.and.be.calledWith(custom, 'key'); | ||
}); | ||
}); |
37602
32
1171
116
16
Updated@babel/runtime@^7.15.4