script-tool

1.0.2 • Public • Published

script-tool

script tool

Install

npm install script-tool

Usage & Api

var st = require("script-tool");

//.getPropertyDescriptor(obj, prop)		//search property descriptor of an object, or of its prototype.
var b = { a: 11 };
var c = Object.create(b);

assert(!Object.getOwnPropertyDescriptor(c, "a"));
assert(!!st.getPropertyDescriptor(c, "a"));

//.enclosePropertyDescriptor(obj, prop, newSetter, newGetter, options)		//enclose property descriptor
var v1 = 0, v2 = 0, v3 = 0;

var b = { get a() { return v1; }, set a(v) { v1 = v; } };
b.a = 1;
assert(v1 === 1 && b.a === 1 && v2 === 0 && v3 === 0);

//new getter is not used
st.enclosePropertyDescriptor(b, "a", function (v) { v2 = v; }, function () { return v3; });
b.a = 2;
assert(v1 === 2 && b.a === 2 && v2 === 2 && v3 === 0);

//old getter is replaced
st.enclosePropertyDescriptor(b, "a", function (v) { v2 = v; }, function () { return v3; }, true);
b.a = 3;
assert(v1 === 3 && b.a === 0 && v2 === 3 && v3 === 0);

//.mapValue(mapper, value)		//try to transfer value by value mapper; if unfound, return original value.
var vm = { a: 11, b: 22 };
assert(st.mapValue(vm, "a") === 11 && st.mapValue(vm, "c") === "c");

var vm = function (v) { if (v == "a") return 11; return 33; }
assert(st.mapValue(vm, "a") === 11 && st.mapValue(vm, "c") === 33);

//.findWithFilter(filter, v /* , v2, ... */)		//find the value that the `filter` will return true; if unfound, return `undefined`

var filter = null;		//default filter exclude null, include only !!v, 0, "" and false.

assert(st.findWithFilter(filter, null, "a", "b") === "a");
assert(st.findWithFilter(filter, null, undefined, 0, "b") === 0);

filter = function (v) { return v == "b" || v === null; };	//only user-defined-value
assert(st.findWithFilter(filter, 0, "a", null, "b") === null);
assert(st.findWithFilter(filter, 0, "a", "b", null) === "b");

assert(typeof st.findWithFilter(filter, 0, "", "a") === "undefined");		//unfound

Readme

Keywords

Package Sidebar

Install

npm i script-tool

Weekly Downloads

0

Version

1.0.2

License

ISC

Unpacked Size

12.4 kB

Total Files

6

Last publish

Collaborators

  • adf0001