Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | 1x 1x 1x 1x 12x 12x 12x 13x 214x 2x 5x 1x 1x 1x 1x 1x 1x 4x 4x 1x 2x 2x 2x 1x 1x 2x 2x 165x 165x 1x 1x 165x 2x 2x 2x 2x 2x 2x 11x 7x 7x 2x 6x 26x 20x 1x 19x 19x 19x 40x 40x 40x 19x 19x 55x 53x 20x 33x 33x 2x 214x 55x 55x 55x 123x 121x 53x 54x 21x 21x 21x 33x 22x 22x 18x 18x 18x 7x 7x 7x 1x 7x 18x 4x 1x | /* * Constants */ const DEFAULT_SETTINGS = Symbol('DefaultSettings'); const RUNTIME_SETTINGS = Symbol('RuntimeSettings'); const OBJECT_SETTINGS_MAP = Symbol('ObjectSettingsMap'); const DICTIONARY = Symbol('Dictionary'); /** * Settings */ export default class Settings { constructor(base?: Settings) { const dictionary = Object.create( base instanceof Settings && DICTIONARY in base ? base[DICTIONARY] : null ); Object.seal( Object.defineProperty(this, DICTIONARY, { value: dictionary, }) ); } set(key: string, value: unknown): boolean { return set(this[DICTIONARY], key, value, null); } get(key: string): unknown { return get(this[DICTIONARY], key); } /** * Unset a specific key or a set of keys within a namespace when the key ends with a dot (ASCII #46). * If the key is ".", all keys will be removed and this command works as a reset. * @param key - name The key to be unset or a namespace. * @returns boolean */ unset(key: string): boolean { return unset(this[DICTIONARY], key + ''); } forEach(callback: (key: string, value: unknown) => void): void { iterate(this[DICTIONARY], callback); } extend(): Settings { return new Settings(this); } /** * Recursively import all properties from the given plain JavaScript object. * This method has the opposite effect of the `dump` method. * @param root - The root object whose properties will * be imported. */ import(root: Record<string, unknown>): void { Eif (isPlainObject(root)) { Object.keys(root).forEach((key) => { set(this[DICTIONARY], key, root[key], null); }); } } /** * Build a JSON representation of the current internal state of this settings * object. The returned object can be safely passed to `JSON.stringify` * function. * @returns The JSON representation of the current * state of this settings instance */ dump(): Record<string, unknown> { const context = {}; iterate(this[DICTIONARY], (key, value) => { Eif (typeof value !== 'undefined') { deepSet(context, key, value); } }); return context; } static assert(subject: Settings): Settings { return subject instanceof Settings ? subject : Settings.getRuntimeSettings(); } static getDefaultSettings(subfield = null): Settings | any { let defaultSettings = Settings[DEFAULT_SETTINGS]; if (!(defaultSettings instanceof Settings)) { defaultSettings = new Settings(); Settings[DEFAULT_SETTINGS] = defaultSettings; } // Given subfield of 'segmentation' it will return all settings // that starts with segmentation.* Iif (subfield) { const settingObj = {}; defaultSettings.forEach((name: string) => { if (name.startsWith(subfield)) { const setting = name.split(`${subfield}.`)[1]; settingObj[setting] = defaultSettings.get(name); } }); return settingObj; } return defaultSettings; } static getRuntimeSettings(): Settings { let runtimeSettings = Settings[RUNTIME_SETTINGS]; if (!(runtimeSettings instanceof Settings)) { runtimeSettings = new Settings(Settings.getDefaultSettings()); Settings[RUNTIME_SETTINGS] = runtimeSettings; } return runtimeSettings; } static getObjectSettings(subject: unknown, from?: unknown): Settings { let settings = null; if (subject instanceof Settings) { settings = subject; } else if (typeof subject === 'object' && subject !== null) { let objectSettingsMap = Settings[OBJECT_SETTINGS_MAP]; if (!(objectSettingsMap instanceof WeakMap)) { objectSettingsMap = new WeakMap(); Settings[OBJECT_SETTINGS_MAP] = objectSettingsMap; } settings = objectSettingsMap.get(subject); if (!(settings instanceof Settings)) { settings = new Settings( Settings.assert(Settings.getObjectSettings(from)) ); objectSettingsMap.set(subject, settings); } } return settings; } static extendRuntimeSettings(): Settings { return Settings.getRuntimeSettings().extend(); } } /* * Local Helpers */ function unset(dictionary: Record<string, unknown>, name: string): boolean { Eif (name.endsWith('.')) { let deleteCount = 0; const namespace = name; const base = namespace.slice(0, -1); const deleteAll = base.length === 0; for (const key in dictionary) { if ( Object.prototype.hasOwnProperty.call(dictionary, key) && (deleteAll || key.startsWith(namespace) || key === base) ) { delete dictionary[key]; ++deleteCount; } } return deleteCount > 0; } return delete dictionary[name]; } function iterate( dictionary: Record<string, unknown>, callback: (key: string, value: unknown) => void ): void { for (const key in dictionary) { callback(key, dictionary[key]); } } function setAll( dictionary: Record<string, unknown>, prefix: string, record: Record<string, unknown>, references: WeakSet<Record<string, unknown>> ): boolean { let failCount: number; if (references.has(record)) { return set(dictionary, prefix, null, references); } references.add(record); failCount = 0; for (const field in record) { Eif (Object.prototype.hasOwnProperty.call(record, field)) { const key = field.length === 0 ? prefix : `${prefix}.${field}`; Iif (!set(dictionary, key, record[field], references)) { ++failCount; } } } references.delete(record); return failCount === 0; } /** * Set the key-value pair on a given dictionary. If the given value is a * plain javascript object, every property of that object will also be set. * @param dictionary {Record<string, unknown>} The target dictionary * @param key {string} The given key * @param value {unknown} The given value * @param references {WeakSet<Record<string, unknown>>} references is a WeakSet * instance used to keep track of which objects have already been iterated * through preventing thus possible stack overflows caused by cyclic references * @returns {boolean} Returns true if every given key-value pair has been * successfully set */ function set( dictionary: Record<string, unknown>, key: string, value: unknown, references: WeakSet<Record<string, unknown>> ): boolean { if (isValidKey(key)) { if (isPlainObject(value)) { return setAll( dictionary, key, value as Record<string, unknown>, references instanceof WeakSet ? references : new WeakSet() ); } dictionary[key] = value; return true; } return false; } function get(dictionary: Record<string, unknown>, key: string): unknown { return dictionary[key]; } /** * Make sure the -provided key correctly formatted. * e.g.: * "my.cool.property" (valid) * "my.cool.property." (invalid) * ".my.cool.property" (invalid) * "my.cool..property" (invalid) * @param key {string} The property name to be used as key within the internal * dictionary * @returns {boolean} True on success, false otherwise */ function isValidKey(key: string): boolean { let last: number, current: number, previous: number; Iif (typeof key !== 'string' || (last = key.length - 1) < 0) return false; previous = -1; while ((current = key.indexOf('.', previous + 1)) >= 0) { if (current - previous < 2 || current === last) return false; previous = current; } return true; } function isPlainObject(subject: unknown) { if (typeof subject === 'object' && subject !== null) { const prototype = Object.getPrototypeOf(subject); Eif (prototype === Object.prototype || prototype === null) { return true; } } return false; } function deepSet(context, key, value) { const separator = key.indexOf('.'); if (separator >= 0) { const subKey = key.slice(0, separator); let subContext = context[subKey]; if (typeof subContext !== 'object' || subContext === null) { const subContextValue = subContext; subContext = {}; if (typeof subContextValue !== 'undefined') { subContext[''] = subContextValue; } context[subKey] = subContext; } deepSet(subContext, key.slice(separator + 1, key.length), value); } else { context[key] = value; } } /** * Initial Settings for the repository */ Settings.getDefaultSettings().set('useCursors', true); |