CoolFace
Apppublic

HarshvardhanCn01/Voice-Assistant

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
object.js244 linesDownload Raw Back to src
1"use strict";2module.exports = ReflectionObject;3 4ReflectionObject.className = "ReflectionObject";5 6var util = require("./util");7 8var Root; // cyclic9 10/**11 * Constructs a new reflection object instance.12 * @classdesc Base class of all reflection objects.13 * @constructor14 * @param {string} name Object name15 * @param {Object.<string,*>} [options] Declared options16 * @abstract17 */18function ReflectionObject(name, options) {19 20    if (!util.isString(name))21        throw TypeError("name must be a string");22 23    if (options && !util.isObject(options))24        throw TypeError("options must be an object");25 26    /**27     * Options.28     * @type {Object.<string,*>|undefined}29     */30    this.options = options; // toJSON31 32    /**33     * Parsed Options.34     * @type {Array.<Object.<string,*>>|undefined}35     */36    this.parsedOptions = null;37 38    /**39     * Unique name within its namespace.40     * @type {string}41     */42    this.name = name;43 44    /**45     * Parent namespace.46     * @type {Namespace|null}47     */48    this.parent = null;49 50    /**51     * Whether already resolved or not.52     * @type {boolean}53     */54    this.resolved = false;55 56    /**57     * Comment text, if any.58     * @type {string|null}59     */60    this.comment = null;61 62    /**63     * Defining file name.64     * @type {string|null}65     */66    this.filename = null;67}68 69Object.defineProperties(ReflectionObject.prototype, {70 71    /**72     * Reference to the root namespace.73     * @name ReflectionObject#root74     * @type {Root}75     * @readonly76     */77    root: {78        get: function() {79            var ptr = this;80            while (ptr.parent !== null)81                ptr = ptr.parent;82            return ptr;83        }84    },85 86    /**87     * Full name including leading dot.88     * @name ReflectionObject#fullName89     * @type {string}90     * @readonly91     */92    fullName: {93        get: function() {94            var path = [ this.name ],95                ptr = this.parent;96            while (ptr) {97                path.unshift(ptr.name);98                ptr = ptr.parent;99            }100            return path.join(".");101        }102    }103});104 105/**106 * Converts this reflection object to its descriptor representation.107 * @returns {Object.<string,*>} Descriptor108 * @abstract109 */110ReflectionObject.prototype.toJSON = /* istanbul ignore next */ function toJSON() {111    throw Error(); // not implemented, shouldn't happen112};113 114/**115 * Called when this object is added to a parent.116 * @param {ReflectionObject} parent Parent added to117 * @returns {undefined}118 */119ReflectionObject.prototype.onAdd = function onAdd(parent) {120    if (this.parent && this.parent !== parent)121        this.parent.remove(this);122    this.parent = parent;123    this.resolved = false;124    var root = parent.root;125    if (root instanceof Root)126        root._handleAdd(this);127};128 129/**130 * Called when this object is removed from a parent.131 * @param {ReflectionObject} parent Parent removed from132 * @returns {undefined}133 */134ReflectionObject.prototype.onRemove = function onRemove(parent) {135    var root = parent.root;136    if (root instanceof Root)137        root._handleRemove(this);138    this.parent = null;139    this.resolved = false;140};141 142/**143 * Resolves this objects type references.144 * @returns {ReflectionObject} `this`145 */146ReflectionObject.prototype.resolve = function resolve() {147    if (this.resolved)148        return this;149    if (this.root instanceof Root)150        this.resolved = true; // only if part of a root151    return this;152};153 154/**155 * Gets an option value.156 * @param {string} name Option name157 * @returns {*} Option value or `undefined` if not set158 */159ReflectionObject.prototype.getOption = function getOption(name) {160    if (this.options)161        return this.options[name];162    return undefined;163};164 165/**166 * Sets an option.167 * @param {string} name Option name168 * @param {*} value Option value169 * @param {boolean} [ifNotSet] Sets the option only if it isn't currently set170 * @returns {ReflectionObject} `this`171 */172ReflectionObject.prototype.setOption = function setOption(name, value, ifNotSet) {173    if (!ifNotSet || !this.options || this.options[name] === undefined)174        (this.options || (this.options = {}))[name] = value;175    return this;176};177 178/**179 * Sets a parsed option.180 * @param {string} name parsed Option name181 * @param {*} value Option value182 * @param {string} propName dot '.' delimited full path of property within the option to set. if undefined\empty, will add a new option with that value183 * @returns {ReflectionObject} `this`184 */185ReflectionObject.prototype.setParsedOption = function setParsedOption(name, value, propName) {186    if (!this.parsedOptions) {187        this.parsedOptions = [];188    }189    var parsedOptions = this.parsedOptions;190    if (propName) {191        // If setting a sub property of an option then try to merge it192        // with an existing option193        var opt = parsedOptions.find(function (opt) {194            return Object.prototype.hasOwnProperty.call(opt, name);195        });196        if (opt) {197            // If we found an existing option - just merge the property value198            var newValue = opt[name];199            util.setProperty(newValue, propName, value);200        } else {201            // otherwise, create a new option, set it's property and add it to the list202            opt = {};203            opt[name] = util.setProperty({}, propName, value);204            parsedOptions.push(opt);205        }206    } else {207        // Always create a new option when setting the value of the option itself208        var newOpt = {};209        newOpt[name] = value;210        parsedOptions.push(newOpt);211    }212    return this;213};214 215/**216 * Sets multiple options.217 * @param {Object.<string,*>} options Options to set218 * @param {boolean} [ifNotSet] Sets an option only if it isn't currently set219 * @returns {ReflectionObject} `this`220 */221ReflectionObject.prototype.setOptions = function setOptions(options, ifNotSet) {222    if (options)223        for (var keys = Object.keys(options), i = 0; i < keys.length; ++i)224            this.setOption(keys[i], options[keys[i]], ifNotSet);225    return this;226};227 228/**229 * Converts this instance to its string representation.230 * @returns {string} Class name[, space, full name]231 */232ReflectionObject.prototype.toString = function toString() {233    var className = this.constructor.className,234        fullName  = this.fullName;235    if (fullName.length)236        return className + " " + fullName;237    return className;238};239 240// Sets up cyclic dependencies (called in index-light)241ReflectionObject._configure = function(Root_) {242    Root = Root_;243};244