DXTR.DEV

CASEX TEMPLATE

Casex Template is a simple template engine that applies the casex pattern to all occurances in a template.

Often creating templates feels tedious and time consuming, I wanted to create an engine that lets someone pretty much copy an example file, replace a few words and you should be good to go.

Source Code

The source code for this project is available on GitHub

Install

Casex Template is currently only available in Javascript (with Typescript types) as a ES Module.

npm install -D @dxtr.dev/casex-template
click to copy

or

yarn add @dxtr.dev/casex-template
click to copy

Usage

{ casexTemplate } is available as a named import, it expects a text and a name. Here's how it looks like:

import { casexTemplate } from '@dxtr.dev/casex-template';

const text = "const __na_me__ = () => '#TODO: Implement Na Me'";
casexTemplate({ text, name: 'cool component' })
click to copy

Common patterns

  • lowercase: __name__
  • UPPERCASE: __NAME__
  • snake_case: __na_me__
  • kebab-case: __na-me__
  • camelCase: __naMe__
  • UpperCamelCase: __NaMe__
  • Sentence case: __Na me__
  • Title Case: __Na Me__

Pluralization

_name_ with only one _ means singular
_name_s with only one _ and s means plural.

Pluralization is handled by pluralize, which also takes care of irregular words.

  • singular: _name_
  • plural: _name_s

Try it out

How it works

Casex Template scans the text for casex-like occurances of _name_ and __name__ uses casex to replaces them according to their Casex pattern.

To get a practical understanding, let's break down the steps, we'll use as an example the name cool component and the text I have many _name_s, my favorite __na-me__ is the _Na Me_.

  1. The text provided gets scanned and we extract the name patterns, resulting in:
    • _name_s - ends with _s so it's plural, with the name pattern (all lowercase)
    • __na-me__ __ double underscore (no plural or singular transformation), with the na-me pattern (kebab-case)
    • _Na Me_ - ends with _ so it's singular, with the NaMe pattern (title case)
  2. The next step is applying the name cool component to each part extracted
    • _name_s: coolcomponents
    • __na-me__: cool-component
    • _Na Me_: Cool Component
  3. Replace the occurances and the result is...
  4. I have many coolcomponents, my favorite cool-component is the Cool Component