@teamawesome/access
Advanced tools
Comparing version 1.0.11 to 1.0.12
@@ -72,2 +72,12 @@ "use strict"; | ||
/** | ||
* Get the amount of properties | ||
* | ||
* @param obj | ||
* @returns {number} | ||
*/ | ||
size: function size(obj) { | ||
return (0, _call.read)(obj, 'size'); | ||
}, | ||
/** | ||
* Get the keys of the object | ||
@@ -74,0 +84,0 @@ * |
@@ -11,2 +11,3 @@ "use strict"; | ||
exports.call2 = call2; | ||
exports.read = read; | ||
@@ -28,3 +29,3 @@ var _types = _interopRequireDefault(require("./types")); | ||
if (obj[method]) { | ||
if (typeof obj[method] === 'function') { | ||
return obj[method](); | ||
@@ -79,2 +80,24 @@ } | ||
throw new TypeError("No [".concat(method, "] handler for objects of type [").concat(obj.constructor.name, "]")); | ||
} | ||
/** | ||
* Call a method on the object's proxy or read the property from the object itself | ||
* | ||
* @param obj | ||
* @param property | ||
* @returns {*} | ||
*/ | ||
function read(obj, property) { | ||
var proxy = _types.default.get(obj.constructor); | ||
if (proxy !== undefined && typeof proxy[property] === 'function') { | ||
return proxy[property](obj); | ||
} | ||
if (property in obj) { | ||
return obj[property]; | ||
} | ||
throw new TypeError("No [".concat(property, "] handler for objects of type [").concat(obj.constructor.name, "]")); | ||
} |
@@ -10,7 +10,7 @@ "use strict"; | ||
require("./array"); | ||
require("./handlers/array"); | ||
require("./object"); | ||
require("./handlers/object"); | ||
require("./storage"); | ||
require("./handlers/storage"); | ||
@@ -17,0 +17,0 @@ var _access = _interopRequireDefault(require("./access")); |
@@ -30,2 +30,8 @@ "use strict"; | ||
if (method in obj) { | ||
return function () { | ||
return obj[method]; | ||
}; | ||
} | ||
throw new TypeError("No [".concat(method.toString(), "] handler for objects of type [").concat(obj.constructor.name, "]")); | ||
@@ -44,2 +50,3 @@ } | ||
var size = get(obj, 'size'); | ||
var wrapped = (0, _defineProperty2.default)({ | ||
@@ -56,2 +63,7 @@ constructor: wrap, | ||
clear: get(obj, 'clear'), | ||
get size() { | ||
return size(); | ||
}, | ||
keys: get(obj, 'keys'), | ||
@@ -58,0 +70,0 @@ values: get(obj, 'values'), |
{ | ||
"name": "@teamawesome/access", | ||
"version": "1.0.11", | ||
"version": "1.0.12", | ||
"description": "Provide a unified interface for objects.", | ||
@@ -44,2 +44,2 @@ "keywords": [ | ||
} | ||
} | ||
} |
@@ -10,3 +10,3 @@ # Access | ||
# Usage | ||
The interface implements get, set, has, delete, clear, keys, values and entries. | ||
The interface implements get, set, has, delete, clear, keys, values, entries and size. | ||
@@ -24,2 +24,3 @@ ``` | ||
access.entries(obj); | ||
access.size(obj); | ||
``` | ||
@@ -42,2 +43,3 @@ Alternatively, wrap an object to provide the interface. An added benefit for this is better performance. Note that if | ||
wrapped[Symbol.iterator](); | ||
wrapped.size; | ||
``` | ||
@@ -44,0 +46,0 @@ |
@@ -1,2 +0,4 @@ | ||
import { call0, call1, call2 } from './call'; | ||
import { | ||
call0, call1, call2, read, | ||
} from './call'; | ||
import types from './types'; | ||
@@ -63,2 +65,12 @@ | ||
/** | ||
* Get the amount of properties | ||
* | ||
* @param obj | ||
* @returns {number} | ||
*/ | ||
size(obj) { | ||
return read(obj, 'size'); | ||
}, | ||
/** | ||
* Get the keys of the object | ||
@@ -65,0 +77,0 @@ * |
@@ -16,3 +16,3 @@ import types from './types'; | ||
if (obj[method]) { | ||
if (typeof obj[method] === 'function') { | ||
return obj[method](); | ||
@@ -66,1 +66,22 @@ } | ||
} | ||
/** | ||
* Call a method on the object's proxy or read the property from the object itself | ||
* | ||
* @param obj | ||
* @param property | ||
* @returns {*} | ||
*/ | ||
export function read(obj, property) { | ||
const proxy = types.get(obj.constructor); | ||
if (proxy !== undefined && typeof proxy[property] === 'function') { | ||
return proxy[property](obj); | ||
} | ||
if (property in obj) { | ||
return obj[property]; | ||
} | ||
throw new TypeError(`No [${property}] handler for objects of type [${obj.constructor.name}]`); | ||
} |
@@ -1,4 +0,4 @@ | ||
import './array'; | ||
import './object'; | ||
import './storage'; | ||
import './handlers/array'; | ||
import './handlers/object'; | ||
import './handlers/storage'; | ||
@@ -5,0 +5,0 @@ export { default, default as access } from './access'; |
@@ -19,2 +19,6 @@ import types from './types'; | ||
if (method in obj) { | ||
return () => obj[method]; | ||
} | ||
throw new TypeError(`No [${method.toString()}] handler for objects of type [${obj.constructor.name}]`); | ||
@@ -31,2 +35,4 @@ } | ||
const set = get(obj, 'set'); | ||
const size = get(obj, 'size'); | ||
const wrapped = { | ||
@@ -43,2 +49,5 @@ constructor: wrap, | ||
clear: get(obj, 'clear'), | ||
get size() { | ||
return size(); | ||
}, | ||
keys: get(obj, 'keys'), | ||
@@ -45,0 +54,0 @@ values: get(obj, 'values'), |
@@ -165,2 +165,22 @@ const should = require('should'); | ||
it('size', () => { | ||
const map = new Map([ | ||
['a', 1], | ||
['b', 2], | ||
]); | ||
access.size(map).should.equal(map.size); | ||
const object = { | ||
a: 1, | ||
b: 2, | ||
}; | ||
access.size(object).should.equal(Object.keys(object).length); | ||
const array = [ | ||
'a', | ||
'b', | ||
]; | ||
access.size(array).should.equal(array.length); | ||
}) | ||
it('implicit type', () => { | ||
@@ -255,3 +275,3 @@ const custom = new class { | ||
// Also symbol.iterator but should cannot display that. | ||
wrapped.should.have.keys('get', 'set', 'has', 'delete', 'clear', 'keys', 'values', 'entries'); | ||
wrapped.should.have.keys('get', 'set', 'has', 'delete', 'clear', 'size', 'keys', 'values', 'entries'); | ||
@@ -258,0 +278,0 @@ wrapped.get('b').should.equal(1); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32418
1044
112
25