mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
refactor: use camelCase for JavaScript APIs
BREAKING CHANGE: Options and method names in JavaScript API are now renamed to cammelCase, for a complete list see #109
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
import { deprecate } from './util/deprecate'
|
|
||||||
import * as _ from './util/underscore'
|
import * as _ from './util/underscore'
|
||||||
|
|
||||||
export interface LiquidOptions {
|
export interface LiquidOptions {
|
||||||
@@ -77,13 +76,6 @@ export function normalize (options?: LiquidOptions): NormalizedOptions {
|
|||||||
if (options.hasOwnProperty('root')) {
|
if (options.hasOwnProperty('root')) {
|
||||||
options.root = normalizeStringArray(options.root)
|
options.root = normalizeStringArray(options.root)
|
||||||
}
|
}
|
||||||
for (const key of Object.keys(options)) {
|
|
||||||
if (key.indexOf('_') > -1) {
|
|
||||||
const newKey = key.replace(/_([a-z])/g, (_, ch) => ch.toUpperCase())
|
|
||||||
deprecate(`${key} is deprecated, use ${newKey} instead.`, 109)
|
|
||||||
options[newKey] = options[key]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return options as NormalizedOptions
|
return options as NormalizedOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,9 +2,7 @@ import { Drop } from '../drop/drop'
|
|||||||
|
|
||||||
type PlainObject = {
|
type PlainObject = {
|
||||||
[key: string]: any
|
[key: string]: any
|
||||||
liquid_method_missing?: (key: string) => any // eslint-disable-line
|
toLiquid?: () => any
|
||||||
to_liquid?: () => any // eslint-disable-line
|
|
||||||
toLiquid?: () => any // eslint-disable-line
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Context = PlainObject | Drop
|
export type Context = PlainObject | Drop
|
||||||
|
|||||||
+1
-1
@@ -22,7 +22,7 @@ export default class Scope {
|
|||||||
async get (path: string) {
|
async get (path: string) {
|
||||||
const paths = await this.propertyAccessSeq(path)
|
const paths = await this.propertyAccessSeq(path)
|
||||||
let ctx = this.findContextFor(paths[0]) || _.last(this.contexts)
|
let ctx = this.findContextFor(paths[0]) || _.last(this.contexts)
|
||||||
for (let path of paths) {
|
for (const path of paths) {
|
||||||
ctx = this.readProperty(ctx, path)
|
ctx = this.readProperty(ctx, path)
|
||||||
if (_.isNil(ctx) && this.opts.strictVariables) {
|
if (_.isNil(ctx) && this.opts.strictVariables) {
|
||||||
throw new TypeError(`undefined variable: ${path}`)
|
throw new TypeError(`undefined variable: ${path}`)
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ export class Filter {
|
|||||||
}
|
}
|
||||||
async render (value: any, scope: Scope) {
|
async render (value: any, scope: Scope) {
|
||||||
const argv: any[] = []
|
const argv: any[] = []
|
||||||
for(let arg of this.args) {
|
for (const arg of this.args) {
|
||||||
if (isArray(arg)) argv.push([arg[0], await evalValue(arg[1], scope)])
|
if (isArray(arg)) argv.push([arg[0], await evalValue(arg[1], scope)])
|
||||||
else argv.push(await evalValue(arg, scope))
|
else argv.push(await evalValue(arg, scope))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ export default class Value {
|
|||||||
}
|
}
|
||||||
async value (scope: Scope) {
|
async value (scope: Scope) {
|
||||||
let val = await evalExp(this.initial, scope)
|
let val = await evalExp(this.initial, scope)
|
||||||
for (let filter of this.filters) {
|
for (const filter of this.filters) {
|
||||||
val = await filter.render(val, scope)
|
val = await filter.render(val, scope)
|
||||||
}
|
}
|
||||||
return val
|
return val
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
const reported:{[key: string]: boolean} = {}
|
|
||||||
|
|
||||||
export function deprecate (msg: string, issue: number) {
|
|
||||||
if (reported[msg]) return
|
|
||||||
console.warn(msg + ` See: https://github.com/harttle/liquidjs/issues/${issue}`)
|
|
||||||
reported[msg] = true
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
import { deprecate } from './deprecate'
|
|
||||||
const toStr = Object.prototype.toString
|
const toStr = Object.prototype.toString
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -29,18 +28,10 @@ export function promisify (fn: any) {
|
|||||||
export function stringify (value: any): string {
|
export function stringify (value: any): string {
|
||||||
if (isNil(value)) return ''
|
if (isNil(value)) return ''
|
||||||
value = toLiquid(value)
|
value = toLiquid(value)
|
||||||
if (isFunction(value.to_s)) {
|
|
||||||
deprecate('to_s is deprecated, use toString instead.', 109)
|
|
||||||
return value.to_s()
|
|
||||||
}
|
|
||||||
return String(value)
|
return String(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function toLiquid (value: any): any {
|
export function toLiquid (value: any): any {
|
||||||
if (isFunction(value.to_liquid)) {
|
|
||||||
deprecate('to_liquid is deprecated, use toLiquid instead.', 109)
|
|
||||||
return toLiquid(value.to_liquid())
|
|
||||||
}
|
|
||||||
if (isFunction(value.toLiquid)) return toLiquid(value.toLiquid())
|
if (isFunction(value.toLiquid)) return toLiquid(value.toLiquid())
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import Liquid from '../../../src/liquid'
|
import Liquid from '../../../src/liquid'
|
||||||
import { expect } from 'chai'
|
import { expect } from 'chai'
|
||||||
|
|
||||||
describe('LiquidOptions#strict_*', function () {
|
describe('LiquidOptions#strict*', function () {
|
||||||
let engine: Liquid
|
let engine: Liquid
|
||||||
const ctx = {}
|
const ctx = {}
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
|
|||||||
@@ -5,35 +5,35 @@ describe('LiquidOptions#trimming', function () {
|
|||||||
const ctx = { name: 'harttle' }
|
const ctx = { name: 'harttle' }
|
||||||
|
|
||||||
describe('tag trimming', function () {
|
describe('tag trimming', function () {
|
||||||
it('should respect trim_tag_left', async function () {
|
it('should respect trimTagLeft', async function () {
|
||||||
const engine = new Liquid({ trim_tag_left: true } as any)
|
const engine = new Liquid({ trimTagLeft: true })
|
||||||
const html = await engine.parseAndRender(' \n \t{%if true%}foo{%endif%} ')
|
const html = await engine.parseAndRender(' \n \t{%if true%}foo{%endif%} ')
|
||||||
return expect(html).to.equal('foo ')
|
return expect(html).to.equal('foo ')
|
||||||
})
|
})
|
||||||
it('should respect trim_tag_right', async function () {
|
it('should respect trimTagRight', async function () {
|
||||||
const engine = new Liquid({ trim_tag_right: true } as any)
|
const engine = new Liquid({ trimTagRight: true } as any)
|
||||||
const html = await engine.parseAndRender('\t{%if true%}foo{%endif%} \n')
|
const html = await engine.parseAndRender('\t{%if true%}foo{%endif%} \n')
|
||||||
return expect(html).to.equal('\tfoo')
|
return expect(html).to.equal('\tfoo')
|
||||||
})
|
})
|
||||||
it('should not trim value', async function () {
|
it('should not trim value', async function () {
|
||||||
const engine = new Liquid({ trim_tag_left: true, trim_tag_right: true } as any)
|
const engine = new Liquid({ trimTagLeft: true, trimTagRight: true } as any)
|
||||||
const html = await engine.parseAndRender('{%if true%}a {{name}} b{%endif%}', ctx)
|
const html = await engine.parseAndRender('{%if true%}a {{name}} b{%endif%}', ctx)
|
||||||
return expect(html).to.equal('a harttle b')
|
return expect(html).to.equal('a harttle b')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
describe('value trimming', function () {
|
describe('value trimming', function () {
|
||||||
it('should respect trim_output_left', async function () {
|
it('should respect trimOutputLeft', async function () {
|
||||||
const engine = new Liquid({ trim_output_left: true } as any)
|
const engine = new Liquid({ trimOutputLeft: true } as any)
|
||||||
const html = await engine.parseAndRender(' \n \t{{name}} ', ctx)
|
const html = await engine.parseAndRender(' \n \t{{name}} ', ctx)
|
||||||
return expect(html).to.equal('harttle ')
|
return expect(html).to.equal('harttle ')
|
||||||
})
|
})
|
||||||
it('should respect trim_output_right', async function () {
|
it('should respect trimOutputRight', async function () {
|
||||||
const engine = new Liquid({ trim_output_right: true } as any)
|
const engine = new Liquid({ trimOutputRight: true } as any)
|
||||||
const html = await engine.parseAndRender(' \n \t{{name}} ', ctx)
|
const html = await engine.parseAndRender(' \n \t{{name}} ', ctx)
|
||||||
return expect(html).to.equal(' \n \tharttle')
|
return expect(html).to.equal(' \n \tharttle')
|
||||||
})
|
})
|
||||||
it('should respect not trim tag', async function () {
|
it('should respect not trim tag', async function () {
|
||||||
const engine = new Liquid({ trim_output_left: true, trim_output_right: true } as any)
|
const engine = new Liquid({ trimOutputLeft: true, trimOutputRight: true } as any)
|
||||||
const html = await engine.parseAndRender('\t{% if true %} aha {%endif%}\t')
|
const html = await engine.parseAndRender('\t{% if true %} aha {%endif%}\t')
|
||||||
return expect(html).to.equal('\t aha \t')
|
return expect(html).to.equal('\t aha \t')
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -85,15 +85,6 @@ describe('scope', function () {
|
|||||||
it("should throw when '' unbalanced", async function () {
|
it("should throw when '' unbalanced", async function () {
|
||||||
expect(scope.get("foo['bar]")).to.be.rejectedWith(/unbalanced '/)
|
expect(scope.get("foo['bar]")).to.be.rejectedWith(/unbalanced '/)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should respect to to_liquid', async function () {
|
|
||||||
const scope = new Scope({ foo: {
|
|
||||||
to_liquid: () => ({ bar: 'BAR' }),
|
|
||||||
bar: 'bar'
|
|
||||||
} })
|
|
||||||
expect(await scope.get('foo.bar')).to.equal('BAR')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should respect to toLiquid', async function () {
|
it('should respect to toLiquid', async function () {
|
||||||
const scope = new Scope({ foo: {
|
const scope = new Scope({ foo: {
|
||||||
toLiquid: () => ({ bar: 'BAR' }),
|
toLiquid: () => ({ bar: 'BAR' }),
|
||||||
|
|||||||
@@ -11,14 +11,6 @@ describe('Output', function () {
|
|||||||
Filter.clear()
|
Filter.clear()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should respect to .to_liquid() method', async function () {
|
|
||||||
const scope = new Scope({
|
|
||||||
bar: { to_liquid: () => 'custom' }
|
|
||||||
})
|
|
||||||
const output = new Output({ value: 'bar' } as OutputToken, false)
|
|
||||||
const html = await output.render(scope)
|
|
||||||
return expect(html).to.equal('custom')
|
|
||||||
})
|
|
||||||
it('should stringify objects', async function () {
|
it('should stringify objects', async function () {
|
||||||
const scope = new Scope({
|
const scope = new Scope({
|
||||||
foo: { obj: { arr: ['a', 2] } }
|
foo: { obj: { arr: ['a', 2] } }
|
||||||
@@ -39,12 +31,6 @@ describe('Output', function () {
|
|||||||
const str = await output.render(scope)
|
const str = await output.render(scope)
|
||||||
return expect(str).to.equal('FOO')
|
return expect(str).to.equal('FOO')
|
||||||
})
|
})
|
||||||
it('should respect to .to_s()', async () => {
|
|
||||||
const scope = new Scope({ obj: { to_s: () => 'FOO' } })
|
|
||||||
const output = new Output({ value: 'obj' } as OutputToken, false)
|
|
||||||
const str = await output.render(scope)
|
|
||||||
return expect(str).to.equal('FOO')
|
|
||||||
})
|
|
||||||
it('should respect to .toString()', async () => {
|
it('should respect to .toString()', async () => {
|
||||||
const scope = new Scope({ obj: { toString: () => 'FOO' } })
|
const scope = new Scope({ obj: { toString: () => 'FOO' } })
|
||||||
const output = new Output({ value: 'obj' } as OutputToken, false)
|
const output = new Output({ value: 'obj' } as OutputToken, false)
|
||||||
|
|||||||
@@ -19,9 +19,6 @@ describe('util/underscore', function () {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
describe('.stringify()', function () {
|
describe('.stringify()', function () {
|
||||||
it('should respect to to_liquid() method', function () {
|
|
||||||
expect(_.stringify({ to_liquid: () => 'foo' })).to.equal('foo')
|
|
||||||
})
|
|
||||||
it('should respect to toLiquid() method', function () {
|
it('should respect to toLiquid() method', function () {
|
||||||
expect(_.stringify({ toLiquid: () => 'foo' })).to.equal('foo')
|
expect(_.stringify({ toLiquid: () => 'foo' })).to.equal('foo')
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user