Skip to content

Commit

Permalink
rename vars to subs (in parsedTemplate)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexewerlof committed Aug 17, 2020
1 parent aacb771 commit 3264a85
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Let's get familiar with a few terms as they are used across this repo:

* **Template**: is a string that contains paths between open and close tags (Example: `Hi {{person.name}}!`).
* **Template**: is a string that contains paths between open and close tags (Example: `Hi {{person.name}}!`). A template is parsed to `strings` (the fixed parts) and `subs` (the substitutions part).
* **Tag** the strings that are used for marking **path**s in the **template**. By default they are '{{' and '}}' respectively
* **Path**: is a string that is used in the **template** to refer to a value (Example: `person.name`)
* **Ref**: is an array that is derived from a **path** (Example: `['person', 'name']`)
Expand Down
2 changes: 1 addition & 1 deletion src/compile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('compile()', () => {
const compiledTokens = compile('Hello {{name}}!')
expect(compiledTokens).toEqual({
strings: ['Hello ', '!'],
vars: [['name']],
subs: [['name']],
})
})
})
20 changes: 10 additions & 10 deletions src/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ describe('parseTemplate()', () => {
it('returns the string intact if no interpolation is found', () => {
expect(parseTemplate('Hello world')).toEqual({
strings: ['Hello world'],
vars: [],
subs: [],
})
})

it('supports customized tags', () => {
expect(parseTemplate('Hello {name}!', { tags: ['{', '}'] })).toEqual({
strings: ['Hello ', '!'],
vars: ['name'],
subs: ['name'],
})
})

Expand All @@ -30,39 +30,39 @@ describe('parseTemplate()', () => {
it('returns an empty string and no paths when the template is an empty string', () => {
expect(parseTemplate('')).toEqual({
strings: [''],
vars: [],
subs: [],
})
})

it('handles interpolation correctly at the start of the template', () => {
expect(parseTemplate('{{name}}! How are you?')).toEqual({
strings: ['', '! How are you?'],
vars: ['name'],
subs: ['name'],
})
})

it('handles interpolation correctly at the end of the template', () => {
expect(parseTemplate('My name is {{name}}')).toEqual({
strings: ['My name is ', ''],
vars: ['name'],
subs: ['name'],
})
})

it('trims path', () => {
const { vars } = parseTemplate('My name is {{ name }}')
if (vars.length) {
expect(vars[0]).toBe('name')
const { subs } = parseTemplate('My name is {{ name }}')
if (subs.length) {
expect(subs[0]).toBe('name')
}
})

it('can handle a close tag without an open tag', () => {
expect(parseTemplate('Hi}} {{name}}')).toEqual({
strings: ['Hi}} ', ''],
vars: ['name'],
subs: ['name'],
})
expect(parseTemplate('Hi {{name}} }}')).toEqual({
strings: ['Hi ', ' }}'],
vars: ['name'],
subs: ['name'],
})
})

Expand Down
12 changes: 6 additions & 6 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ export interface ParsedTemplate<T> {
*/
strings: string[]
/**
* An array corresponding to the variable part of the template.
* An array corresponding to the substitute part of the template.
*
* [[parseTemplate]] gives an array of strings while [[compile]] gives an array of [[Ref]]s which
* are also arrays. Furthermore, any transformation acts on this data structure and may put
* whatever item types in this array.
*
* If there are no paths in the template, this will be an empty array.
*/
vars: T[]
subs: T[]
}

/**
Expand All @@ -43,9 +43,9 @@ export function isParsedTemplate(x: unknown): x is ParsedTemplate<any> {
}
const parsedTemplate = x as ParsedTemplate<any>
return (
isArr(parsedTemplate?.strings) &&
isArr(parsedTemplate?.vars) &&
parsedTemplate.strings.length === parsedTemplate.vars.length + 1
isArr(parsedTemplate.strings) &&
isArr(parsedTemplate.subs) &&
parsedTemplate.strings.length === parsedTemplate.subs.length + 1
)
}

Expand Down Expand Up @@ -121,7 +121,7 @@ function pureParser(

strings.push(template.substring(lastCloseTagIndex))

return { strings, vars: paths }
return { strings, subs: paths }
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ export function stringify(
}

const { explicit } = options
const { strings, vars } = parsedTemplate
const { length } = vars
const { strings, subs } = parsedTemplate
const { length } = subs

let ret = ''
for (let i = 0; i < length; i++) {
ret += strings[i]

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const value: any = vars[i]
const value: any = subs[i]

if (explicit || (value !== null && value !== undefined)) {
ret += value
Expand Down Expand Up @@ -101,7 +101,7 @@ export function render(
options?: RenderOptions
): string {
const parsedTemplate = isStr(template) ? compile(template, options) : template
const resolvedTemplate = isArr(parsedTemplate.vars[0])
const resolvedTemplate = isArr(parsedTemplate.subs[0])
? // eslint-disable-next-line @typescript-eslint/no-unsafe-return
transform(parsedTemplate, (ref: Ref) => refGet(ref, scope, options))
: // eslint-disable-next-line @typescript-eslint/no-unsafe-return
Expand Down
2 changes: 1 addition & 1 deletion src/tokenize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface TokenizePathOptions {
* - `'person.firstName'` which gives us `['person', 'firstName']` as a Ref
* - `'person.lastName'` which gives us `['person', 'lastName']` as a Ref
*
* Therefore the `vars` property will be `[['person', 'firstName'], ['person', 'lastName']`
* Therefore the `subs` property will be `[['person', 'firstName'], ['person', 'lastName']`
*/
export type Ref = string[]

Expand Down
8 changes: 4 additions & 4 deletions src/transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ describe('transform()', () => {
it('applies the transformation to the parsedTemplate', () => {
const parsedTemplate: ParsedTemplate<string> = {
strings: ['A', 'C', 'E'],
vars: ['B', 'D'],
subs: ['B', 'D'],
}
expect(transform(parsedTemplate, (path, index) => `${path.toLowerCase()}-${index}`)).toEqual({
strings: ['A', 'C', 'E'],
vars: ['b-0', 'd-1'],
subs: ['b-0', 'd-1'],
})
})
})
Expand All @@ -26,7 +26,7 @@ describe('transformAsync()', () => {
it('applies the transformation to the parsedTemplate', async () => {
const parsedTemplate: ParsedTemplate<string> = {
strings: ['A', 'C', 'E'],
vars: ['B', 'D'],
subs: ['B', 'D'],
}
expect(
await transformAsync(
Expand All @@ -35,7 +35,7 @@ describe('transformAsync()', () => {
)
).toEqual({
strings: ['A', 'C', 'E'],
vars: ['b-0', 'd-1'],
subs: ['b-0', 'd-1'],
})
})
})
10 changes: 5 additions & 5 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ export function transform<T, R>(
throw new TypeError(`Invalid parsed template: ${parsedTemplate}`)
}

const { strings, vars } = parsedTemplate
const { strings, subs } = parsedTemplate

const transformedVars = vars.map(transformer)
const transformedSubs = subs.map(transformer)

return { strings, vars: transformedVars }
return { strings, subs: transformedSubs }
}

export async function transformAsync<T, R>(
parsedTemplate: ParsedTemplate<T>,
transformer: (value: T, index: number, array: T[]) => Promise<R>
): Promise<ParsedTemplate<R>> {
const { strings, vars } = transform(parsedTemplate, transformer)
return { strings, vars: await Promise.all(vars) }
const { strings, subs } = transform(parsedTemplate, transformer)
return { strings, subs: await Promise.all(subs) }
}

0 comments on commit 3264a85

Please sign in to comment.