{"_id":"@beppobert/ts-template","name":"@beppobert/ts-template","dist-tags":{"latest":"0.0.1"},"versions":{"0.0.1":{"name":"@beppobert/ts-template","version":"0.0.1","private":false,"author":{"name":"Philipp Dehler"},"license":"MIT","devDependencies":{"ts-node":"^10.9.1","typescript":"^5.3.3","vitest":"^1.2.2"},"main":"dist/index.js","types":"dist/index.d.ts","scripts":{"test":"vitest"},"repository":{"type":"git","url":"git+https://github.com/Beppobert/ts-template.git"},"keywords":["typescript","template","template engine"],"_id":"@beppobert/ts-template@0.0.1","gitHead":"87022552e5d7035200451d343949468253c16701","description":"This is a small customizable typesafe template engine written in Typescript.","bugs":{"url":"https://github.com/Beppobert/ts-template/issues"},"homepage":"https://github.com/Beppobert/ts-template#readme","_nodeVersion":"18.14.0","_npmVersion":"9.8.0","dist":{"integrity":"sha512-wgRl2dMFfPXPoRXspgoOiasrusyuuqlRomvCgB3JYFiWzwSKYwLxIobyJjguIGauvQ/maGC0KvIkGwwnbNfdgw==","shasum":"f5993cb7067c5aef2ff3bd6a6cadb898c64f9047","tarball":"https://registry.npmjs.org/@beppobert/ts-template/-/ts-template-0.0.1.tgz","fileCount":84,"unpackedSize":173150,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDVY5M/8PafGG+swDxFelHG9ZtGml3izmxgsEUTrOVtsAIhAMuczlGy1pfXF1mX4PJyxPW4+M2hZWOSjdNSC4cLTvzF"}]},"_npmUser":{"name":"beppobert","email":"philipp.dehler@googlemail.com"},"directories":{},"maintainers":[{"name":"beppobert","email":"philipp.dehler@googlemail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/ts-template_0.0.1_1706625297080_0.44879190549030845"},"_hasShrinkwrap":false}},"time":{"created":"2024-01-30T14:34:56.964Z","0.0.1":"2024-01-30T14:34:57.290Z","modified":"2024-01-30T14:34:57.508Z"},"maintainers":[{"name":"beppobert","email":"philipp.dehler@googlemail.com"}],"description":"This is a small customizable typesafe template engine written in Typescript.","homepage":"https://github.com/Beppobert/ts-template#readme","keywords":["typescript","template","template engine"],"repository":{"type":"git","url":"git+https://github.com/Beppobert/ts-template.git"},"author":{"name":"Philipp Dehler"},"bugs":{"url":"https://github.com/Beppobert/ts-template/issues"},"license":"MIT","readme":"# template-ts\n\nThis is a small customizable typesafe template engine written in Typescript.\n\n## Setup\n\n## Quickstart\n\n```shell\nnpm i @beppobert/ts-template\n```\n\n```typescript\nimport {\n  TypeDefinition,\n  ArgDefinition,\n  OperationDefinition,\n  Pattern,\n  createSchema,\n  template,\n} from \"@beppobert/ts-template\";\n\n// create your custom types\n\nconst stringType = new TypeDefinition(\"string\", {\n  validator: (input: string) => typeof input === \"string\",\n  parseValue: (value): string => value,\n  pattern: new Pattern<`\"${string}\"`, '\"\"'>(/^\\\"(.*)\\\"$/),\n});\nconst numberType = new TypeDefinition(\"number\", {\n  validator: (input: number) => typeof input === \"number\",\n  parseValue: (value): number => Number(value),\n  pattern: new Pattern<`${number}`, \"0\">(new RegExp(`^${Number}$`)),\n});\n\n// create your custom operations\n\nconst uppercaseOperation = new OperationDefinition(\"upper\", {\n  args: [],\n  input: stringType,\n  return: stringType,\n  operation: (input) => input.toUpperCase(),\n});\n\nconst replaceOperation = new OperationDefinition(\"replace\", {\n  args: [\n    new ArgDefinition(\"searchValue\", stringType),\n    new ArgDefinition(\"replaceValue\", stringType),\n  ],\n  input: stringType,\n  return: stringType,\n  operation: (input, { searchValue, replaceValue }) =>\n    input.replace(searchValue, replaceValue),\n});\n\nconst toString = new OperationDefinition(\"toString\", {\n  args: [],\n  input: numberType,\n  return: stringType,\n  operation: (input) => input.toString(),\n});\n\n// create your  schema\n\nconst schema = createSchema(\n  {\n    types: [stringType, numberType],\n    defaultType: \"string\",\n  },\n  {\n    // group the operation based on their input type into an object\n    string: [uppercaseOperation, replaceOperation],\n    number: [toString],\n  }\n);\n\n// create your template function\n\nconst templateFn = template(schema);\n\nconst templateString = templateFn(\"Hello {{name|upper}}\"); // enjoy auto completion\n```\n\n## Create a custom type\n\n```typescript\nimport { TypeDefinition, Pattern } from \"@beppobert/ts-template\";\n/***\n * The Pattern class defines a pattern that your string in your arguments has to match\n * The first generic will define the typescript side of your pattern.\n * Any arg of type string will be checked against this type.\n * e.g. {{...|...|replace(\"a\",\"b\")}} <- \"a\" and \"b\" have to match the pattern\n *\n * The second generic will define a default value if the pattern doesn't match.\n *\n * The pattern regex will be used to validate your arg string.\n * It's last match should also return the value you want to parse.\n *\n * /\nconst strPattern = new Pattern<`\"${string}\"`, '\"\"'>(/^\\\"(.*)\\\"$/);\n\nconst stringType = new TypeDefinition(\"string\", {\n  pattern: strPattern,\n  // is called after the parsing step\n  validator: (input: string) => typeof input === \"string\",\n\n  // receives the last match of the pattern regex and must return a value of the desired type\n  parseValue: (value): string => value,\n});\n\n```\n\n## Create a custom operation\n\n```typescript\nimport { OperationDefinition, ArgDefinition } from \"@beppobert/ts-template\";\n/**\n * The first argument is the key of your operation.\n * It will be used in your template string to call your operation.\n */\nconst replace = new OperationDefinition(\"replace\", {\n  // define the arguments of your operation\n  args: [\n    new ArgDefinition(\"searchValue\", stringType),\n    new ArgDefinition(\"replaceValue\", stringType),\n  ],\n  // define the input type of your operation\n  input: stringType,\n  // define the return type of your operation\n  return: stringType,\n  // define the operation\n  operation: (input, { searchValue, replaceValue }) =>\n    input.replace(searchValue, replaceValue),\n});\n```\n\n## Create a custom schema\n\nThe schema runs multiple runtime checks to ensure that your schema is valid.\n\n```typescript\nimport { createSchema } from \"@beppobert/ts-template\";\n\nconst schema = createSchema(\n  {\n    // define your types\n    types: [stringType, numberType],\n    // define your default type\n    defaultType: \"string\",\n  },\n  {\n    // group the operation based on their input type into an object\n    string: [uppercaseOperation, replaceOperation],\n    number: [toString],\n  }\n);\n```\n\n## Create a custom template function\n\n```typescript\nimport { template } from \"@beppobert/ts-template\";\n\nconst templateFn = template(schema);\n/**\n * A value you want to get replaced needs to be wrapped in {{...}}\n *\n * The first part of the string is the name of the value you want to replace.\n * If the value is optional you can add a ? before the name.\n * e.g. {{?name}} will be replaced with the value of name if it exists\n *\n * The second part of the string is the type of the value.\n * The type is prefixed with #. If it's not prefixed the default type will be used.\n * e.g. {{name#string}} is the same as {{name}} if string is the default type\n *\n * the third part of the string is the operations you want to call on the value.\n * e.g. {{name#string|upper|replace(\"a\",\"b\")}}. The typesytem ensures that after each operation you can only * call operations that are defined for the return type of the previous operation.\n * e.g.{{name#string|upper|toString}} will result in an error\n */\nconst templateString = templateFn(\n  'Hello {{name#string|upper|replace(\"O\",\"i\")}}'\n); // enjoy auto completion\ntemplateString({ name: \"Bob\" }); // Hello BiB\n\nconst templateString2 = templateFn(\"Hello {{name|upper}}\");\n```\n","readmeFilename":"README.md"}