ern-api-gen
Version:
Electrode Native API generator
171 lines • 4.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const StringUtils_1 = require("./StringUtils");
/**
* Takes a prototype and a list of property names and creates getters and
* setters.
*
* @param prototype
* @param p
* @returns {*}
*/
exports.beanify = (prototype, p, prefix = '') => {
const props = Array.isArray(p) ? p : p == null ? [] : [p];
for (const pr of props) {
let op = pr;
let pre = prefix;
let defValue;
if (Array.isArray(pr)) {
op = pr[0];
if (pr.length > 1) {
defValue = pr[1];
}
if (pr.length > 2) {
pre = pr[2];
}
}
const prop = `${pre}${op}`;
const uProp = StringUtils_1.upperFirst(op);
const set = `set${uProp}`;
const get = `get${uProp}`;
if (!exports.has(prototype, set)) {
prototype[set] = function (value) {
this[prop] = value;
};
}
if (!exports.has(prototype, get)) {
prototype[get] = function () {
return exports.has(this, prop)
? this[prop]
: typeof defValue === 'function'
? defValue()
: defValue;
};
}
}
return prototype;
};
/**
* Takes a bean and applies the values where the values have
* setters the setters are called.
*/
function resolve(obj, path) {
if (obj == null) {
return;
}
if (path == null) {
return obj;
}
if (typeof obj[path] === 'function') {
return obj[path]();
}
if (path in obj) {
return obj[path];
}
if (obj instanceof Map) {
return obj.get(path);
}
}
function canResolve(obj, path) {
return true;
if (obj == null) {
return false;
}
if (path == null) {
return false;
}
if (typeof path !== 'string') {
return false;
}
if (obj instanceof Map) {
return obj.has(path);
}
return Object.hasOwnProperty.call(obj, path);
}
function each(obj, keys, fn, scope) {
if (obj == null) {
return obj;
}
if (typeof obj[Symbol.iterator] === 'function') {
for (const [k, v] of obj) {
if (!keys || keys.indexOf(k) > -1) {
fn.call(scope, v, k);
}
}
}
keys = keys || Object.keys(obj);
for (const k of keys) {
fn.call(scope, obj[k], k);
}
}
/**
* Apply however, it only sets a value that has a property or a setter to invoke.
* @param bean
* @param obj
*/
exports.applyStrict = (bean, obj) => {
return exports.apply(bean, obj, null, true);
};
exports.canResolveNoFunc = (obj, prop) => {
if (!canResolve(obj, prop)) {
return false;
}
return typeof obj[prop] !== 'function';
};
exports.apply = (bean, obj, properties = null, strict = false) => {
if (obj == null) {
return bean;
}
if (bean == null) {
throw new Error(`Bean can not be null`);
}
const prefix = '';
each(obj, properties, (v, p) => {
let op = p;
let pre = prefix;
let defValue;
if (Array.isArray(p)) {
op = p[0];
if (p.length > 1) {
defValue = p[1];
}
if (p.length > 2) {
pre = p[2];
}
}
const prop = `${pre}${op}`;
const uProp = StringUtils_1.upperFirst(op);
const set = `set${uProp}`;
const is = `is${uProp}`;
const get = `get${uProp}`;
if (canResolve(obj, prop) ||
typeof obj[get] === 'function' ||
typeof obj[is] === 'function') {
const value = typeof obj[get] === 'function'
? obj[get]()
: typeof obj[is] === 'function'
? obj[is]()
: resolve(obj, prop);
if (typeof bean[set] === 'function') {
bean[set](value);
}
else if (!strict || exports.canResolveNoFunc(bean, prop)) {
bean[prop] = value;
}
}
});
return bean;
};
exports.has = (obj, ...properties) => {
if (obj == null) {
return false;
}
for (const p of properties) {
if (!(p in obj)) {
return false;
}
}
return true;
};
exports.default = { beanify: exports.beanify, apply: exports.apply, has: exports.has };
//# sourceMappingURL=beanUtils.js.map