temject

0.2.4 • Public • Published

temject

template + injection = temject

'Hello, {{name:pascal}}! Today is {{__date}}'  
--> "Hello, World! Today is 2018-12-04"

Build Status Build status FOSSA Status

lukeed/templite: Lightweight templating in 150 bytes

Allows you to denote dynamic portions of a string using double curly brackets ({{ example }}) & then replace them with matching values from your data source.

You may attach an Object or an Array as your data source, which means you may use the object's keys or the array's indices to assign values.

Lastly, you may use dot-notated paths to access (deeply) nested values; eg: foo.bar.baz, 0.0.0, or foo.0.1.bar.

templite('Hello, {{name}}!', { name: 'world' });
//=> Hello, world!

temjecttempliteの機能を備えています。加えて以下の機能があります。

  • テンプレート文字列をパスカルケースやスネークケース等に変換できます
  • テンプレート文字列をdatetime(実行時の日付や時刻)に変換できます

blakeembrey/change-case

Convert strings between camelCase, PascalCase, Title Case, snake_case, lowercase, UPPERCASE, CONSTANT_CASE and more.

const x = 'Hello, {{name:pascal}}! Today is {{__date}}'
const y = { name: 'world' }
temject(x, y)
// -> "Hello, World! Today is 2018-12-04"

install

npm install --save temject

Usage

const { temject } = require('temject')
const x = `
// createdAt: {{__date}}
// path/to/MyProject/{{name}}.js
 
class {{name:pascal}} {}`
 
const y = { name: 'myFunction' }
temject(x, y)
 
// ->
/*
// createdAt: 2018-12-04
// path/to/MyProject/myFunction.js
 
class MyFunction {}
*/

API - templite

templite本来のapiは以下を参照願います。 (templiteの部分をtemjectに変えるのをお忘れなく)

templite: API

Do not forget to change the templite part totemject.

API - temject original

temject(input, values)

下記のkeyValueInjectordateTimeInjectorをまとめて実行します。特に分ける必要がなければこの関数だけでtemjectの機能は網羅しています。

// example
const { temject } = require('temject')
const x = 'Hello, {{name:pascal}}! Today is {{__date}}'
const y = { name: 'world' }
// -> ' "Hello, World! Today is 2018-12-04"

keyValueInjector(input, values)

templiteの機能はそのまま使えます。さらにchange-case関数で変換する機能を加えています

テンプレート文字列を:で区切ると変換関数を指定できます。例えばパスカルケースに変換するには{{name:pascal}}とします。

You can specify a conversion function by separating the template string with :. For example, to convert it to a Pascal case, we use {{name:pascal}}.

// example
'Hello, {{name:pascal}}!'

変換関数はchange-caseモジュールを使っています。以下の関数名を指定できます。

The conversion function uses the change-case module. The following function names can be specified.

change-case: usage

camel
constant
dot
header
isLower
isUpper
lower
lcFirst
no
param
pascal
path
sentence
snake
swap
title
upper
ucFirst

Example

temject('{{key: camel}}', { key: 'helloWorld' })
/* change the first argument
{{key: camel}} :  helloWorld 
{{key: constant}} :  HELLO_WORLD 
{{key: dot}} :  hello.world 
{{key: header}} :  Hello-World 
{{key: isLower}} :  false 
{{key: isUpper}} :  false 
{{key: lower}} :  helloworld 
{{key: lcFirst}} :  helloWorld 
{{key: no}} :  hello world 
{{key: param}} :  hello-world 
{{key: pascal}} :  HelloWorld 
{{key: path}} :  hello/world 
{{key: sentence}} :  Hello world 
{{key: snake}} :  hello_world 
{{key: swap}} :  HELLOwORLD 
{{key: title}} :  Hello World 
{{key: upper}} :  HELLOWORLD 
{{key: ucFirst}} :  HelloWorld
*/
// example
const {keyValueInjector} = require('temject')
keyValueInjector('Hello, {{name:pascal}}!', {name: 'mick'})
// -> Hello, Mick!

dateTimeInjector(input)

テンプレート文字列内部の先頭を__(アンダースコア2つ)で始めるとdatetimeを注入できます。

You can inject a datetime by beginning the inside of the template string with __ (two underscores).

以下の文字列で日付が注入できます。

You can inject dates with the following strings:

'{{__date}}'

他にも以下の日付形式が使用できます。

The following date formats can be used.

{{__year}} 2018 
{{__date}} 2018-12-04 
{{__Date}} 2018/12/04 
{{__datetime}} 2018-12-04 20:05:24 
{{__Datetime}} 2018/12/04 20:05:24 
{{__month}} 2018-12 
{{__Month}} 2018/12 

また、 date-and-timeのformatが使用できます。

You can also use date-and-time format.

{{__ddd MMM DD YYYY}} :  Tue Dec 04 2018 
{{__hh:mm A [GMT]Z}} :  08:21 p.m. GMT+0900 
Today is {{__dddd}} :  Today is Tuesday

おまけ機能ですが、簡単なhashも生成できます

{{__hash8}} :  3mwe4xGQ 
{{__hash16}} :  VaAv3mwe4xGQj1qB 
{{__hash24}} :  dLy0VaAv3mwe4xGQj1qBrKM4
// example
const { dateTimeInjector } = require('temject')
dateTimeInjector('{{0:camel}}{{0:pascal}}{{0:dot}}', ['toDo'])
// -> 'toDoToDoto.do'

expressions(input)

template内全てのkeyを取得します。 重複は省かれます。

const {expressions} = require('temject');
const str = '{{name}}{{name:pascal}}{{age}}'
expressions(str)
// -> [ 'name', 'age' ]
expressions(str, { ignore: ['name'] })
// -> [ 'age' ]

License

MIT © mick-whats

FOSSA Status

Package Sidebar

Install

npm i temject

Weekly Downloads

5

Version

0.2.4

License

MIT

Unpacked Size

13 kB

Total Files

9

Last publish

Collaborators

  • whats