|
| 1 | +/** |
| 2 | + * Name: prefixer.ts |
| 3 | + * Description: Configuration object key prefixer |
| 4 | + * Author: Ovidiu Barabula <lectii2008@gmail.com> |
| 5 | + * @since 0.1.0 |
| 6 | + */ |
| 7 | + |
| 8 | +import { Config } from '../config-manager'; |
| 9 | +import { required } from '../util/utility-functions'; |
| 10 | + |
| 11 | + |
| 12 | +export enum PrefixOperation { |
| 13 | + Remove, |
| 14 | + Add, |
| 15 | +} |
| 16 | + |
| 17 | +export interface ConfigPrefixer { |
| 18 | + apply(config: Config): Config; |
| 19 | + remove(config: Config): Config; |
| 20 | + performOperation?(type: PrefixOperation, config: Config): Config; |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +// Custom error messages |
| 25 | +export const ERRORS = { |
| 26 | + CONFIG_REQUIRED: 'ConfigPrefixer> apply() and remove() methods require first parameter <config> of type \'object\'', |
| 27 | + PREFIX_REQUIRED: 'ConfigPrefixer() requires first parameter <prefix> of type \'string\'', |
| 28 | + UNKNOWN_OPERATION: 'ConfigPrefixer> An unknonw operation was passed to performOperation() method', |
| 29 | +}; |
| 30 | + |
| 31 | + |
| 32 | +/** |
| 33 | + * Prefix config keys with custom string |
| 34 | + * @param prefix Prefix string to be added to each key |
| 35 | + */ |
| 36 | +function ConfigPrefixer( |
| 37 | + prefix: string = required(ERRORS.PREFIX_REQUIRED), |
| 38 | +): ConfigPrefixer { |
| 39 | + /** |
| 40 | + * Check if key already has prefix applied |
| 41 | + * @param key Key string |
| 42 | + */ |
| 43 | + function hasPrefix(key: string): boolean { |
| 44 | + return key.includes(prefix) && key.indexOf(prefix) === 0; |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + /** |
| 49 | + * Add prefix to key |
| 50 | + * @param key Object key string |
| 51 | + */ |
| 52 | + function addPrefix(key: string): string { |
| 53 | + return !hasPrefix(key) ? `${prefix}${key}` : key; |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + /** |
| 58 | + * Remove prefix from key |
| 59 | + * @param key Object key string |
| 60 | + */ |
| 61 | + function removePrefix(key: string): string { |
| 62 | + return hasPrefix(key) ? key.substring(prefix.length) : key; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + /** |
| 67 | + * Perform an operation on passed in configuration object |
| 68 | + * @param type Type of the operation |
| 69 | + * @param config Configuration object |
| 70 | + */ |
| 71 | + function performOperation(type: PrefixOperation, config: Config): Config { |
| 72 | + let operation: (key: string) => string; |
| 73 | + |
| 74 | + if (type === PrefixOperation.Add) { |
| 75 | + operation = (key: string) => addPrefix(key); |
| 76 | + } else if (type === PrefixOperation.Remove) { |
| 77 | + operation = (key: string) => removePrefix(key); |
| 78 | + } else { |
| 79 | + throw new Error(ERRORS.UNKNOWN_OPERATION); |
| 80 | + } |
| 81 | + |
| 82 | + return Object.keys(config) |
| 83 | + .reduce((newConfig, key) => ({ |
| 84 | + ...newConfig, |
| 85 | + ...{ [operation(key)]: config[key] }, |
| 86 | + }), {}); |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + /** |
| 91 | + * Apply prefix to object keys |
| 92 | + * @param config Configuration object |
| 93 | + */ |
| 94 | + function apply(config: Config = required(ERRORS.CONFIG_REQUIRED)): Config { |
| 95 | + return performOperation(PrefixOperation.Add, config); |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + /** |
| 100 | + * Remove prefix from object keys |
| 101 | + * @param config Prefixed configuration object |
| 102 | + */ |
| 103 | + function remove(config: Config = required(ERRORS.CONFIG_REQUIRED)): Config { |
| 104 | + return performOperation(PrefixOperation.Remove, config); |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + // Creating the public API object |
| 109 | + let publicApi: ConfigPrefixer = { |
| 110 | + apply, |
| 111 | + remove, |
| 112 | + }; |
| 113 | + |
| 114 | + // Adding private methods to public API in test environment |
| 115 | + /* test:start */ |
| 116 | + publicApi = {...publicApi, |
| 117 | + performOperation, |
| 118 | + }; |
| 119 | + /* test:end */ |
| 120 | + |
| 121 | + // Returning config wizard public API |
| 122 | + return Object.freeze(publicApi); |
| 123 | +} |
| 124 | + |
| 125 | +export default ConfigPrefixer; |
0 commit comments