Package Exports
- proxy-pants
- proxy-pants/accessor
- proxy-pants/bound
- proxy-pants/breadcrumbs
- proxy-pants/extender
- proxy-pants/function
- proxy-pants/package.json
Readme
proxy-pants
Social Media Photo by lan deng on Unsplash
Secured and reliable Proxy based utilities for more or less common tasks:
- accessor to trap one or more accessors for any object
- applier & caller to trap any borrowed callback/utility without needing to use
.call
or.apply
to pass the context - bound to bind one or more methods all at once
- bread & crumbs to track operations through paths (i.e.
a.b.c.d
) and namespaces - extender to extend any object through weakly referenced behaviors, providing a new way to deal with state machines too, through the following features:
- methods are always the same bound reference
- properties are defined per extender and never directly attached to the source
- accessors are also defined per each extender
- multiple extenders calls to the same source preserve previous state, and any source can pass through multiple extenders without ever conflicting
accessor
// import {accessor} from 'proxy-pants/accessor';
import {accessor} from 'proxy-pants';
const {textContent} = accessor(document.body);
// get the current body text
textContent();
// set the new one
textContent('proxy pants!');
applier & caller
// import {applier, caller} from 'proxy-pants/function';
import {applier, caller} from 'proxy-pants';
const {hasOwnProperty, toString} = caller(Object.prototype);
// true
hasOwnProperty({any: 'object'}, 'any');
// [object Null]
toString(null);
const {fromCharCode} = applier(String);
const charCodes = (...args) => fromCharCode(String, args);
// <=>
charCodes(60, 61, 62);
bound
// import {bound} from 'proxy-pants/bound';
import {bound} from 'proxy-pants';
const map = new Map;
const {get, set, has} = bound(map);
// false
has('some');
// the map
set('some', 'value');
// true
has('some');
// 'value'
get('some');
bread & crumbs
// import {bread, crumbs} from 'proxy-pants/breadcrumbs';
import {bread, crumbs} from 'proxy-pants';
const namespace = {
some: 'value',
method(...args) {
return this.some + args.length;
},
Class: class {}
};
const facade = crumbs({
apply(path, args) {
return bread(namespace, path)(...args);
},
construct(path, args) {
const Class = bread(namespace, path);
return new Class;
},
get(path, key) {
return bread(namespace, path)[key];
},
has(path, key) {
return key in bread(namespace, path);
},
set(path, key, value) {
bread(namespace, path)[key] = value;
return true;
},
// alias for deleteProperty(path, key) {}
delete(path, key) {
return delete bread(namespace, path)[key];
}
});
facade.some; // value
facade.method(1, 2, 3); // some3
new facade.Class; // [object Namespace]
'some' in facade; // true
facade.test = 'ok';
facade.test; // ok
delete facade.test; // true
extender
// import {extender} from 'proxy-pants/extender';
import {extender} from 'proxy-pants';
const Magic = extender({
// properties are per extender and weakly related
isMagic: true,
// accessors context is the original source/target
get magic() {
// this === source
return Magic(this).isMagic;
},
// methods are always same bound method reference
hasMagic() {
// this === source
return Magic(this).magic;
}
});
const source = {};
const target = Magic(source);
magic.isMagic; // true
magic.magic; // true
magic.hasMagic; // true