|
| 1 | +/* |
| 2 | + * Copyright 2016 Wave Software |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +export interface UniqIdGenerator { |
| 18 | + generateUniqId(): string; |
| 19 | +} |
| 20 | + |
| 21 | +class JFormatter { |
| 22 | + private readonly template: string; |
| 23 | + |
| 24 | + constructor(template: string) { |
| 25 | + this.template = template; |
| 26 | + } |
| 27 | + |
| 28 | + format(args: unknown[]): string { |
| 29 | + const regex = /%s/; |
| 30 | + const reducer = (p: string, c: unknown): string => p.replace(regex, String(c)); |
| 31 | + return args.reduce(reducer, this.template) as string; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +class MathRandom { |
| 36 | + private static readonly LONG_MAX = 2 ** 53 - 1; |
| 37 | + private static readonly LONG_MIN = -1 * 2 ** 53; |
| 38 | + |
| 39 | + nextLong(): number { |
| 40 | + return this.nextNumber(MathRandom.LONG_MIN, MathRandom.LONG_MAX); |
| 41 | + } |
| 42 | + |
| 43 | + nextInt(max: number): number { |
| 44 | + return this.nextNumber(0, max); |
| 45 | + } |
| 46 | + |
| 47 | + nextNumber(min: number, max: number): number { |
| 48 | + return Math.floor(Math.random() * (max - min + 1)) + min; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +class StdUniqIdGenerator implements UniqIdGenerator { |
| 53 | + private static readonly BASE36 = 36; |
| 54 | + private static readonly INTEGER_MAX_VALUE = 2 ** 31; |
| 55 | + private static readonly MIN_6_CHAR_HASH = 60466177; // for '100001' in base36 |
| 56 | + |
| 57 | + private readonly padding: string; |
| 58 | + private readonly generator: MathRandom; |
| 59 | + |
| 60 | + constructor() { |
| 61 | + this.generator = new MathRandom(); |
| 62 | + this.padding = this.generator |
| 63 | + .nextNumber(StdUniqIdGenerator.MIN_6_CHAR_HASH, StdUniqIdGenerator.INTEGER_MAX_VALUE) |
| 64 | + .toString(StdUniqIdGenerator.BASE36); |
| 65 | + } |
| 66 | + |
| 67 | + generateUniqId(): string { |
| 68 | + const first = Math.abs(this.generator.nextLong() + 1); |
| 69 | + const second = Math.abs(this.generator.nextInt(StdUniqIdGenerator.INTEGER_MAX_VALUE)); |
| 70 | + const calc = first + second; |
| 71 | + const hash = (Math.abs(calc) % StdUniqIdGenerator.INTEGER_MAX_VALUE).toString( |
| 72 | + StdUniqIdGenerator.BASE36, |
| 73 | + ); |
| 74 | + return String(this.padding + hash).slice(-6); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +const DEFAULT_FORMAT = "[%s]<%s>"; |
| 79 | +const DEFAULT_REF_FORMAT = "[%s|%s]<%s>"; |
| 80 | +const DEFAULT_MESSAGE_FORMAT = "%s => %s"; |
| 81 | +const DEFAULT_UNIQ_ID_GENERATOR: UniqIdGenerator = new StdUniqIdGenerator(); |
| 82 | + |
| 83 | +const FORMAT_NUM_SPEC = 2; |
| 84 | +const REF_FORMAT_NUM_SPEC = 3; |
| 85 | +const MESSAGE_FORMAT_NUM_SPEC = 2; |
| 86 | + |
| 87 | +interface EidState { |
| 88 | + format: string; |
| 89 | + refFormat: string; |
| 90 | + messageFormat: string; |
| 91 | + uniqIdGenerator: UniqIdGenerator; |
| 92 | +} |
| 93 | + |
| 94 | +const EidInternal: EidState = { |
| 95 | + format: DEFAULT_FORMAT, |
| 96 | + refFormat: DEFAULT_REF_FORMAT, |
| 97 | + messageFormat: DEFAULT_MESSAGE_FORMAT, |
| 98 | + uniqIdGenerator: DEFAULT_UNIQ_ID_GENERATOR, |
| 99 | +}; |
| 100 | + |
| 101 | +function validateFormat(format: string | null | undefined, numSpecifiers: number): void { |
| 102 | + if (format === null || format === undefined) { |
| 103 | + throw new TypeError("Format can't be null, but just received one"); |
| 104 | + } |
| 105 | + const specifiers: string[] = []; |
| 106 | + for (let i = 0; i < numSpecifiers; i++) { |
| 107 | + specifiers.push(`${i}-test-id`); |
| 108 | + } |
| 109 | + const formatted = new JFormatter(format).format(specifiers); |
| 110 | + for (const specifier of specifiers) { |
| 111 | + if (formatted.indexOf(specifier) === -1) { |
| 112 | + throw new TypeError( |
| 113 | + `Given format contains to little format specifiers, expected ${numSpecifiers} but given "${format}"`, |
| 114 | + ); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Exception identifier for all Eid Runtime Exceptions. |
| 121 | + * |
| 122 | + * This class shouldn't be used in any public API or library. It is designed |
| 123 | + * to be used for in-house development of end user applications which will |
| 124 | + * report Bugs in standardized error pages or post them to issue tracker. |
| 125 | + */ |
| 126 | +export class Eid { |
| 127 | + static readonly DEFAULT_FORMAT: string = DEFAULT_FORMAT; |
| 128 | + static readonly DEFAULT_REF_FORMAT: string = DEFAULT_REF_FORMAT; |
| 129 | + static readonly DEFAULT_MESSAGE_FORMAT: string = DEFAULT_MESSAGE_FORMAT; |
| 130 | + static readonly DEFAULT_UNIQ_ID_GENERATOR: UniqIdGenerator = DEFAULT_UNIQ_ID_GENERATOR; |
| 131 | + |
| 132 | + private readonly id: string; |
| 133 | + private readonly ref: string; |
| 134 | + private readonly uniq: string; |
| 135 | + |
| 136 | + constructor(id: string, ref?: string | null) { |
| 137 | + this.id = id; |
| 138 | + this.ref = ref === null || ref === undefined ? "" : ref; |
| 139 | + this.uniq = EidInternal.uniqIdGenerator.generateUniqId(); |
| 140 | + } |
| 141 | + |
| 142 | + toString(): string { |
| 143 | + if (this.ref === "") { |
| 144 | + return new JFormatter(EidInternal.format).format([this.id, this.uniq]); |
| 145 | + } |
| 146 | + return new JFormatter(EidInternal.refFormat).format([this.id, this.ref, this.uniq]); |
| 147 | + } |
| 148 | + |
| 149 | + getId(): string { |
| 150 | + return this.id; |
| 151 | + } |
| 152 | + |
| 153 | + getRef(): string { |
| 154 | + return this.ref; |
| 155 | + } |
| 156 | + |
| 157 | + getUniq(): string { |
| 158 | + return this.uniq; |
| 159 | + } |
| 160 | + |
| 161 | + makeLogMessage(logMessageFormat: string, ...params: unknown[]): string { |
| 162 | + const message = new JFormatter(logMessageFormat).format(params); |
| 163 | + return new JFormatter(Eid.getMessageFormat()).format([this.toString(), message]); |
| 164 | + } |
| 165 | + |
| 166 | + static setFormat(format: string): string { |
| 167 | + validateFormat(format, FORMAT_NUM_SPEC); |
| 168 | + const previously = EidInternal.format; |
| 169 | + EidInternal.format = format; |
| 170 | + return previously; |
| 171 | + } |
| 172 | + |
| 173 | + static setRefFormat(refFormat: string): string { |
| 174 | + validateFormat(refFormat, REF_FORMAT_NUM_SPEC); |
| 175 | + const previously = EidInternal.refFormat; |
| 176 | + EidInternal.refFormat = refFormat; |
| 177 | + return previously; |
| 178 | + } |
| 179 | + |
| 180 | + static setMessageFormat(format: string): string { |
| 181 | + validateFormat(format, MESSAGE_FORMAT_NUM_SPEC); |
| 182 | + const oldFormat = EidInternal.messageFormat; |
| 183 | + EidInternal.messageFormat = format; |
| 184 | + return oldFormat; |
| 185 | + } |
| 186 | + |
| 187 | + static getMessageFormat(): string { |
| 188 | + return EidInternal.messageFormat; |
| 189 | + } |
| 190 | + |
| 191 | + static setUniqIdGenerator(uniqIdGenerator: UniqIdGenerator): UniqIdGenerator { |
| 192 | + if (uniqIdGenerator === null || uniqIdGenerator === undefined) { |
| 193 | + throw new TypeError("Unique ID generator can't be null, but given one"); |
| 194 | + } |
| 195 | + const previous = EidInternal.uniqIdGenerator; |
| 196 | + EidInternal.uniqIdGenerator = uniqIdGenerator; |
| 197 | + return previous; |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +export default Eid; |
0 commit comments