UNPKG

@toreda/strong-types

Version:

Better TypeScript code in fewer lines.

1 lines 4.91 kB
{"version":3,"sources":["../src/transforms.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAAC,SAAS,EAAC,MAAM,aAAa,CAAC;AACtC,OAAO,EAAC,WAAW,EAAC,MAAM,gBAAgB,CAAC;AAE3C;;;;;;GAMG;AACH,qBAAa,UAAU,CAAC,CAAC;IACxB,SAAgB,UAAU,EAAE,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;IAC3C,SAAgB,YAAY,EAAE,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC;IAC/C,SAAgB,eAAe,EAAE,CAAC,CAAC;gBAEvB,eAAe,EAAE,CAAC;IAM9B;;;;;;OAMG;IACI,KAAK,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,OAAO;IAShD;;;;;;OAMG;IACI,GAAG,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,OAAO;IAU5C;;;;;OAKG;IACI,GAAG,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC;IAiBvB;;;;;OAKG;IACI,KAAK,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI,GAAG,CAAC,GAAG,IAAI;IAiBvC;;;OAGG;IACI,KAAK,IAAI,IAAI;CAIpB","file":"transforms.d.ts","sourcesContent":["/**\n *\tMIT License\n *\n *\tCopyright (c) 2019 - 2021 Toreda, Inc.\n *\n *\tPermission is hereby granted, free of charge, to any person obtaining a copy\n *\tof this software and associated documentation files (the \"Software\"), to deal\n *\tin the Software without restriction, including without limitation the rights\n *\tto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n *\tcopies of the Software, and to permit persons to whom the Software is\n *\tfurnished to do so, subject to the following conditions:\n\n * \tThe above copyright notice and this permission notice shall be included in all\n * \tcopies or substantial portions of the Software.\n *\n * \tTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n *\tIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n *\tFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * \tAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n *\tLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n *\tOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * \tSOFTWARE.\n *\n */\n\nimport {Transform} from './transform';\nimport {TransformNB} from './transform/nb';\n\n/**\n * Container holding the transform function chain used to transform\n * values before saving. Transforms are applied in the order they are\n * stored in the transforms or transformsNB array.\n *\n * @category Transforms\n */\nexport class Transforms<T> {\n\tpublic readonly transforms: Transform<T>[];\n\tpublic readonly transformsNB: TransformNB<T>[];\n\tpublic readonly fallbackDefault: T;\n\n\tconstructor(fallbackDefault: T) {\n\t\tthis.transforms = [];\n\t\tthis.transformsNB = [];\n\t\tthis.fallbackDefault = fallbackDefault;\n\t}\n\n\t/**\n\t * Add nullable transform function to chain.\n\t * @param transform\t\tNullable transform fn to be added to chain.\n\t * @returns\t\t\t\tBoolean indicating add success or failure.\n\t *\t\t\t\t\t\ttrue\t-\tTransform fn added to chain successfully.\n\t *\t\t\t\t\t\tfalse\t-\tTransform fn not added to chain.\n\t */\n\tpublic addNB(transform: TransformNB<T>): boolean {\n\t\tif (!transform) {\n\t\t\treturn false;\n\t\t}\n\n\t\tthis.transformsNB.push(transform);\n\t\treturn true;\n\t}\n\n\t/**\n\t * Add transform function to chain.\n\t * @param transform\t\tTransform fn to be added to chain.\n\t * @returns\t\t\t\tBoolean indicating add success or failure.\n\t *\t\t\t\t\t\ttrue\t-\tTransform fn added to chain successfully.\n\t *\t\t\t\t\t\tfalse\t-\tTransform fn not added to chain.\n\t */\n\tpublic add(transform: Transform<T>): boolean {\n\t\tif (!transform) {\n\t\t\treturn false;\n\t\t}\n\n\t\t// todo: add sorted insert here based on filter.sortOrder\n\t\tthis.transforms.push(transform);\n\t\treturn true;\n\t}\n\n\t/**\n\t * Run transform chain, applying each transforms once the order added to\n\t * the chain.\n\t * @param value\n\t * @returns\n\t */\n\tpublic run(value: T): T {\n\t\tif (typeof value === 'undefined' || value === null) {\n\t\t\treturn this.fallbackDefault;\n\t\t}\n\n\t\tlet transformed: T = value;\n\t\tconst transforms = this.transforms;\n\n\t\tfor (const transform of transforms) {\n\t\t\tconst input = transformed;\n\t\t\tconst output = transform.run(input);\n\t\t\ttransformed = output;\n\t\t}\n\n\t\treturn transformed;\n\t}\n\n\t/**\n\t * Run transform chain, applying each transforms once the order added to\n\t * the chain.\n\t * @param value\n\t * @returns\n\t */\n\tpublic runNB(value: T | null): T | null {\n\t\tif (typeof value === 'undefined' || value === null) {\n\t\t\treturn null;\n\t\t}\n\n\t\tlet transformed: T | null = value;\n\n\t\tconst transformsNB = this.transformsNB;\n\t\tfor (const transform of transformsNB) {\n\t\t\tconst input = transformed;\n\t\t\tconst output = transform.run(input);\n\t\t\ttransformed = output;\n\t\t}\n\n\t\treturn transformed;\n\t}\n\n\t/**\n\t * Remove all transform functions from chain. Fallback value\n\t * remains the same.\n\t */\n\tpublic reset(): void {\n\t\tthis.transforms.length = 0;\n\t\tthis.transformsNB.length = 0;\n\t}\n}\n"]}