@teamawesome/access
Advanced tools
Comparing version 1.0.6 to 1.0.7
module.exports = { | ||
"parser": "babel-eslint", | ||
"extends": "airbnb-base", | ||
"rules": { | ||
"no-param-reassign": "off" | ||
} | ||
}; | ||
parser: 'babel-eslint', | ||
extends: 'airbnb-base', | ||
rules: { | ||
'no-param-reassign': 'off', | ||
}, | ||
}; |
@@ -32,3 +32,3 @@ const suite = (new require('benchmark').Suite)(); | ||
results.forEach((result) => { | ||
console.log(`${result.target.name}\t\t\t${result.target.hz.toLocaleString('en-US', { maximumFractionDigits: 0 })}`); | ||
console.log(`${result.target.name.padEnd(15, ' ')}${result.target.hz.toLocaleString('en-US', { maximumFractionDigits: 0 })}`); | ||
}); | ||
@@ -35,0 +35,0 @@ }) |
@@ -32,3 +32,3 @@ const suite = (new require('benchmark').Suite)(); | ||
results.forEach((result) => { | ||
console.log(`${result.target.name}\t\t\t${result.target.hz.toLocaleString('en-US', { maximumFractionDigits: 0 })}`); | ||
console.log(`${result.target.name.padEnd(15, ' ')}${result.target.hz.toLocaleString('en-US', { maximumFractionDigits: 0 })}`); | ||
}); | ||
@@ -35,0 +35,0 @@ }) |
@@ -35,8 +35,8 @@ "use strict"; | ||
/** | ||
* Get a value by key | ||
* | ||
* @param obj | ||
* @param key | ||
* @return {*} | ||
*/ | ||
* Get a value by key | ||
* | ||
* @param obj | ||
* @param key | ||
* @return {*} | ||
*/ | ||
get: function get(obj, key) { | ||
@@ -47,10 +47,10 @@ return call(obj, 'get', key); | ||
/** | ||
* Set the value of a key | ||
* | ||
* @template T | ||
* @param {T} obj | ||
* @param key | ||
* @param value | ||
* @return {T} | ||
*/ | ||
* Set the value of a key | ||
* | ||
* @template T | ||
* @param {T} obj | ||
* @param key | ||
* @param value | ||
* @return {T} | ||
*/ | ||
set: function set(obj, key, value) { | ||
@@ -62,7 +62,8 @@ call(obj, 'set', key, value); | ||
/** | ||
* Does the object contain the key | ||
* @param obj | ||
* @param key | ||
* @return {bool} | ||
*/ | ||
* Does the object contain the key | ||
* | ||
* @param obj | ||
* @param key | ||
* @return {bool} | ||
*/ | ||
has: function has(obj, key) { | ||
@@ -73,7 +74,8 @@ return call(obj, 'has', key); | ||
/** | ||
* Delete the key | ||
* @param obj | ||
* @param key | ||
* @return {bool} | ||
*/ | ||
* Delete the key | ||
* | ||
* @param obj | ||
* @param key | ||
* @return {bool} | ||
*/ | ||
"delete": function _delete(obj, key) { | ||
@@ -84,6 +86,6 @@ return call(obj, 'delete', key); | ||
/** | ||
* Remove all entries | ||
* | ||
* @param obj | ||
*/ | ||
* Remove all entries | ||
* | ||
* @param obj | ||
*/ | ||
clear: function clear(obj) { | ||
@@ -94,7 +96,37 @@ call(obj, 'clear'); | ||
/** | ||
* Register a type | ||
* | ||
* @param {*} type | ||
* @param {{}} proxy | ||
*/ | ||
* Get the keys of the object | ||
* | ||
* @param obj | ||
* @return {IterableIterator} | ||
*/ | ||
keys: function keys(obj) { | ||
return call(obj, 'keys'); | ||
}, | ||
/** | ||
* Get the values of the object | ||
* | ||
* @param obj | ||
* @return {IterableIterator} | ||
*/ | ||
values: function values(obj) { | ||
return call(obj, 'values'); | ||
}, | ||
/** | ||
* Get the entries of the object | ||
* | ||
* @param obj | ||
* @return {IterableIterator} | ||
*/ | ||
entries: function entries(obj) { | ||
return call(obj, 'entries'); | ||
}, | ||
/** | ||
* Register a type | ||
* | ||
* @param {*} type | ||
* @param {{}} proxy | ||
*/ | ||
register: function register(type, proxy) { | ||
@@ -105,6 +137,6 @@ types.set(type, proxy); | ||
/** | ||
* Remove a type | ||
* | ||
* @param type | ||
*/ | ||
* Remove a type | ||
* | ||
* @param type | ||
*/ | ||
unregister: function unregister(type) { | ||
@@ -111,0 +143,0 @@ types["delete"](type); |
@@ -5,5 +5,7 @@ "use strict"; | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); | ||
var _access = _interopRequireDefault(require("./access")); | ||
var proxy = { | ||
var proxy = (0, _defineProperty2["default"])({ | ||
get: function get(obj, key) { | ||
@@ -25,4 +27,15 @@ return obj[key]; | ||
}); | ||
}, | ||
keys: function keys(obj) { | ||
return Object.keys(obj).values(); | ||
}, | ||
values: function values(obj) { | ||
return Object.values(obj).values(); | ||
}, | ||
entries: function entries(obj) { | ||
return Object.entries(obj).values(); | ||
} | ||
}; | ||
}, Symbol.iterator, function (obj) { | ||
return Object.entries(obj).values(); | ||
}); | ||
@@ -29,0 +42,0 @@ _access["default"].register(Object, proxy); // Objects created without a prototype do not have a constructor |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -8,2 +10,4 @@ value: true | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); | ||
var _access = require("./access"); | ||
@@ -13,3 +17,3 @@ | ||
* @param {*} obj | ||
* @param {string} method | ||
* @param {string|Symbol} method | ||
* @return {*} | ||
@@ -28,3 +32,3 @@ */ | ||
throw new TypeError("No [".concat(method, "] handler for objects of type [").concat(obj.constructor.name, "]")); | ||
throw new TypeError("No [".concat(method.toString(), "] handler for objects of type [").concat(obj.constructor.name, "]")); | ||
} | ||
@@ -42,3 +46,3 @@ /** | ||
var wrapped = { | ||
var wrapped = (0, _defineProperty2["default"])({ | ||
constructor: wrap, | ||
@@ -53,5 +57,8 @@ get: get(obj, 'get'), | ||
"delete": get(obj, 'delete'), | ||
clear: get(obj, 'clear') | ||
}; | ||
clear: get(obj, 'clear'), | ||
keys: get(obj, 'keys'), | ||
values: get(obj, 'values'), | ||
entries: get(obj, 'entries') | ||
}, Symbol.iterator, get(obj, Symbol.iterator)); | ||
return wrapped; | ||
} |
{ | ||
"name": "@teamawesome/access", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "Provide an unified interface for objects.", | ||
@@ -26,20 +26,20 @@ "keywords": [ | ||
"devDependencies": { | ||
"@babel/cli": "^7.6.4", | ||
"@babel/core": "^7.6.4", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.6.2", | ||
"@babel/plugin-transform-runtime": "^7.6.2", | ||
"@babel/preset-env": "^7.6.3", | ||
"babel-eslint": "^10.0.3", | ||
"@babel/cli": "^7.10.5", | ||
"@babel/core": "^7.11.1", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.11.0", | ||
"@babel/plugin-transform-runtime": "^7.11.0", | ||
"@babel/preset-env": "^7.11.0", | ||
"babel-eslint": "^10.1.0", | ||
"benchmark": "^2.1.4", | ||
"eslint": "^4.19.1", | ||
"eslint-config-airbnb-base": "^13.2.0", | ||
"eslint-plugin-import": "^2.18.2", | ||
"mocha": "^5.2.0", | ||
"eslint": "^7.7.0", | ||
"eslint-config-airbnb-base": "^14.2.0", | ||
"eslint-plugin-import": "^2.22.0", | ||
"mocha": "^8.1.1", | ||
"should": "^13.2.3", | ||
"should-sinon": "0.0.6", | ||
"sinon": "^6.3.5" | ||
"sinon": "^9.0.3" | ||
}, | ||
"dependencies": { | ||
"@babel/runtime": "^7.6.3" | ||
"@babel/runtime": "^7.11.2" | ||
} | ||
} |
@@ -7,3 +7,3 @@ # Installation | ||
# Usage | ||
The interface implements get, set, has, delete and clear. | ||
The interface implements get, set, has, delete, clear, keys, values and entries. | ||
@@ -18,5 +18,8 @@ ``` | ||
access.clear(obj, key); | ||
access.keys(obj); | ||
access.values(obj); | ||
access.entries(obj); | ||
``` | ||
Alternatively, wrap an object to provide the interface. An added benefit for this is better performance. Note that if | ||
a proxy is neccesary for the object, it must be registered before wrapping. | ||
a proxy is necessary for the object, it must be registered before wrapping. | ||
``` | ||
@@ -32,2 +35,6 @@ import wrap from '@teamawesome/access/wrap' | ||
wrapped.clear(); | ||
wrapped.keys(); | ||
wrapped.values(); | ||
wrapped.entries(); | ||
wrapped[Symbol.iterator](); | ||
``` | ||
@@ -34,0 +41,0 @@ |
@@ -16,3 +16,5 @@ export const types = new Map(); | ||
return proxy[method](obj, key, value); | ||
} if (typeof obj[method] === 'function') { | ||
} | ||
if (typeof obj[method] === 'function') { | ||
return obj[method](key, value); | ||
@@ -26,8 +28,8 @@ } | ||
/** | ||
* Get a value by key | ||
* | ||
* @param obj | ||
* @param key | ||
* @return {*} | ||
*/ | ||
* Get a value by key | ||
* | ||
* @param obj | ||
* @param key | ||
* @return {*} | ||
*/ | ||
get(obj, key) { | ||
@@ -38,10 +40,10 @@ return call(obj, 'get', key); | ||
/** | ||
* Set the value of a key | ||
* | ||
* @template T | ||
* @param {T} obj | ||
* @param key | ||
* @param value | ||
* @return {T} | ||
*/ | ||
* Set the value of a key | ||
* | ||
* @template T | ||
* @param {T} obj | ||
* @param key | ||
* @param value | ||
* @return {T} | ||
*/ | ||
set(obj, key, value) { | ||
@@ -54,7 +56,8 @@ call(obj, 'set', key, value); | ||
/** | ||
* Does the object contain the key | ||
* @param obj | ||
* @param key | ||
* @return {bool} | ||
*/ | ||
* Does the object contain the key | ||
* | ||
* @param obj | ||
* @param key | ||
* @return {bool} | ||
*/ | ||
has(obj, key) { | ||
@@ -65,7 +68,8 @@ return call(obj, 'has', key); | ||
/** | ||
* Delete the key | ||
* @param obj | ||
* @param key | ||
* @return {bool} | ||
*/ | ||
* Delete the key | ||
* | ||
* @param obj | ||
* @param key | ||
* @return {bool} | ||
*/ | ||
delete(obj, key) { | ||
@@ -76,6 +80,6 @@ return call(obj, 'delete', key); | ||
/** | ||
* Remove all entries | ||
* | ||
* @param obj | ||
*/ | ||
* Remove all entries | ||
* | ||
* @param obj | ||
*/ | ||
clear(obj) { | ||
@@ -86,7 +90,37 @@ call(obj, 'clear'); | ||
/** | ||
* Register a type | ||
* | ||
* @param {*} type | ||
* @param {{}} proxy | ||
*/ | ||
* Get the keys of the object | ||
* | ||
* @param obj | ||
* @return {IterableIterator} | ||
*/ | ||
keys(obj) { | ||
return call(obj, 'keys'); | ||
}, | ||
/** | ||
* Get the values of the object | ||
* | ||
* @param obj | ||
* @return {IterableIterator} | ||
*/ | ||
values(obj) { | ||
return call(obj, 'values'); | ||
}, | ||
/** | ||
* Get the entries of the object | ||
* | ||
* @param obj | ||
* @return {IterableIterator} | ||
*/ | ||
entries(obj) { | ||
return call(obj, 'entries'); | ||
}, | ||
/** | ||
* Register a type | ||
* | ||
* @param {*} type | ||
* @param {{}} proxy | ||
*/ | ||
register(type, proxy) { | ||
@@ -97,6 +131,6 @@ types.set(type, proxy); | ||
/** | ||
* Remove a type | ||
* | ||
* @param type | ||
*/ | ||
* Remove a type | ||
* | ||
* @param type | ||
*/ | ||
unregister(type) { | ||
@@ -103,0 +137,0 @@ types.delete(type); |
@@ -19,4 +19,16 @@ import access from './access'; | ||
.keys(obj) | ||
.forEach(key => delete obj[key]); | ||
.forEach((key) => delete obj[key]); | ||
}, | ||
keys(obj) { | ||
return Object.keys(obj).values(); | ||
}, | ||
values(obj) { | ||
return Object.values(obj).values(); | ||
}, | ||
entries(obj) { | ||
return Object.entries(obj).values(); | ||
}, | ||
[Symbol.iterator](obj) { | ||
return Object.entries(obj).values(); | ||
}, | ||
}; | ||
@@ -23,0 +35,0 @@ |
@@ -5,3 +5,3 @@ import { types } from './access'; | ||
* @param {*} obj | ||
* @param {string} method | ||
* @param {string|Symbol} method | ||
* @return {*} | ||
@@ -18,3 +18,3 @@ */ | ||
throw new TypeError(`No [${method}] handler for objects of type [${obj.constructor.name}]`); | ||
throw new TypeError(`No [${method.toString()}] handler for objects of type [${obj.constructor.name}]`); | ||
} | ||
@@ -41,2 +41,6 @@ | ||
clear: get(obj, 'clear'), | ||
keys: get(obj, 'keys'), | ||
values: get(obj, 'values'), | ||
entries: get(obj, 'entries'), | ||
[Symbol.iterator]: get(obj, Symbol.iterator), | ||
}; | ||
@@ -43,0 +47,0 @@ |
@@ -108,2 +108,37 @@ const should = require('should'); | ||
it('iterators', () => { | ||
const map = new Map([ | ||
['a', 1], | ||
['b', 2], | ||
]); | ||
access.keys(map).should.be.iterator(); | ||
access.values(map).should.be.iterator(); | ||
access.entries(map).should.be.iterator(); | ||
[...access.keys(map)].should.eql(['a', 'b']); | ||
[...access.values(map)].should.eql([1, 2]); | ||
[...access.entries(map)].should.containDeep([['a', 1], ['b', 2]]); | ||
const object = { | ||
a: 1, | ||
b: 2, | ||
}; | ||
access.keys(object).should.be.iterator(); | ||
access.values(object).should.be.iterator(); | ||
access.entries(object).should.be.iterator(); | ||
[...access.keys(object)].should.eql(['a', 'b']); | ||
[...access.values(object)].should.eql([1, 2]); | ||
[...access.entries(object)].should.containDeep([['a', 1], ['b', 2]]); | ||
const array = [ | ||
'a', | ||
'b', | ||
]; | ||
access.keys(array).should.be.iterator(); | ||
access.values(array).should.be.iterator(); | ||
access.entries(array).should.be.iterator(); | ||
[...access.keys(array)].should.eql([0, 1]); | ||
[...access.values(array)].should.eql(['a', 'b']); | ||
[...access.entries(array)].should.containDeep([[0, 'a'], [1, 'b']]); | ||
}); | ||
it('clear', () => { | ||
@@ -205,2 +240,5 @@ const map = new Map([ | ||
// Also symbol.iterator but should cannot display that. | ||
wrapped.should.have.keys('get', 'set', 'has', 'delete', 'clear', 'keys', 'values', 'entries'); | ||
wrapped.get('b').should.equal(1); | ||
@@ -215,4 +253,11 @@ | ||
[...wrapped].should.deepEqual([['b', 1]]); | ||
wrapped.clear(); | ||
wrapped.has('b').should.equal(false); | ||
wrapped.keys().should.be.iterator(); | ||
wrapped.values().should.be.iterator(); | ||
wrapped.entries().should.be.iterator(); | ||
wrapped.should.be.iterable(); | ||
}); |
22699
740
82
Updated@babel/runtime@^7.11.2