anyid
Version:
A simple and flexible API to generate various kinds of string ID / code.
47 lines • 1.84 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const assert_1 = __importDefault(require("assert"));
const _ = __importStar(require("lodash"));
const core_1 = require("./core");
class VariableValue extends core_1.Value {
constructor(owner, name) {
super(owner);
this.name = name;
}
value(arg) {
assert_1.default(!_.isUndefined(arg), 'Variable requires to be given in id()');
if (typeof arg === 'number' || arg instanceof Buffer) {
assert_1.default(!this.name, 'Expect an object to be passed into id()');
return this.returnValue(arg);
}
else {
if (this.name) {
assert_1.default(_.has(arg, this.name), `Missing property ${this.name} in object passed into id()`);
const property = arg[this.name];
if (typeof property === 'number' || property instanceof Buffer) {
return this.returnValue(property);
}
throw new Error(`Expect variable ${this.name} to be a number or a Buffer`);
}
throw new Error('Expect a number or a Buffer to be passed into id() instead of object');
}
}
}
class Variable {
variable(name) {
this.addValue(new VariableValue(this, name));
return this;
}
}
exports.Variable = Variable;
//# sourceMappingURL=variable.js.map