UNPKG

@metamask/logger

Version:

A lightweight logging package using @metamask/streams

1 lines 3.19 kB
{"version":3,"file":"options.mjs","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,yBAAwB;AAGnD;;GAEG;AACH,MAAM,CAAC,MAAM,eAAe,GAA4B;IACtD,UAAU,EAAE,EAAE;IACd,KAAK,EAAE,MAAM;IACb,IAAI,EAAE,EAAE;CACT,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAC1B,OAA2C,EAC5B,EAAE;IACjB,qEAAqE;IACrE,0EAA0E;IAC1E,QAAQ,OAAO,OAAO,EAAE,CAAC;QACvB,KAAK,QAAQ;YACX,OAAO,OAAO,CAAC;QACjB,KAAK,QAAQ;YACX,OAAO,EAAE,IAAI,EAAE,CAAC,OAAO,CAAC,EAAE,UAAU,EAAE,CAAC,gBAAgB,CAAC,EAAE,CAAC;QAC7D,KAAK,WAAW;YACd,OAAO,EAAE,UAAU,EAAE,CAAC,gBAAgB,CAAC,EAAE,CAAC;QAC5C;YACE,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;IAC9C,CAAC;AACH,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,CAAU,KAAgB,EAAa,EAAE;IAC7D,OAAO,KAAK,CAAC,MAAM,CACjB,CAAC,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,KAAK,CAC1D,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAC1B,GAAG,OAAwB,EACF,EAAE,CAC3B,OAAO,CAAC,MAAM,CACZ,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,CACd,CAAC;IACC,UAAU,EAAE,MAAM,CAAC,CAAC,GAAG,GAAG,CAAC,UAAU,EAAE,GAAG,CAAC,MAAM,CAAC,UAAU,IAAI,EAAE,CAAC,CAAC,CAAC;IACrE,KAAK,EAAE,MAAM,CAAC,KAAK,IAAI,GAAG,CAAC,KAAK;IAChC,IAAI,EAAE,MAAM,CAAC,CAAC,GAAG,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC;CACpD,CAA4B,EAC/B,eAAe,CAChB,CAAC","sourcesContent":["import { consoleTransport } from './transports.ts';\nimport type { LoggerOptions } from './types.ts';\n\n/**\n * The default options for the logger.\n */\nexport const DEFAULT_OPTIONS: Required<LoggerOptions> = {\n transports: [],\n level: 'info',\n tags: [],\n};\n\n/**\n * Parses the options for the logger.\n *\n * @param options - The options for the logger.\n * @returns The parsed options.\n */\nexport const parseOptions = (\n options: LoggerOptions | string | undefined,\n): LoggerOptions => {\n // The default case catches whatever is not explicitly handled below.\n // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check\n switch (typeof options) {\n case 'object':\n return options;\n case 'string':\n return { tags: [options], transports: [consoleTransport] };\n case 'undefined':\n return { transports: [consoleTransport] };\n default:\n throw new Error('Invalid logger options');\n }\n};\n\n/**\n * Returns a copy of an array containing only its unique values.\n *\n * @param array - The array to filter.\n * @returns The array, without duplicate values.\n */\nexport const unique = <Element>(array: Element[]): Element[] => {\n return array.filter(\n (element, index, self) => self.indexOf(element) === index,\n );\n};\n\n/**\n * Merges multiple logger options into a single options object.\n *\n * @param options - The options to merge.\n * @returns The merged options.\n */\nexport const mergeOptions = (\n ...options: LoggerOptions[]\n): Required<LoggerOptions> =>\n options.reduce<Required<LoggerOptions>>(\n (acc, option) =>\n ({\n transports: unique([...acc.transports, ...(option.transports ?? [])]),\n level: option.level ?? acc.level,\n tags: unique([...acc.tags, ...(option.tags ?? [])]),\n }) as Required<LoggerOptions>,\n DEFAULT_OPTIONS,\n );\n"]}