mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-14 20:00:39 -07:00
fix: math filters now return number, resolves #110
* fix linting
* numbers are no longer fixed:
i.e. {{0.3 | minus 0.1}} now renders as 0.19999
Stick to JavaScript number behavior, see: 8.0.0
This commit is contained in:
+1
-1
@@ -18,6 +18,6 @@
|
||||
"prefer-const": 2,
|
||||
"no-unused-vars": "off",
|
||||
"import/export": "off",
|
||||
"@typescript-eslint/no-unused-vars": ["error", { "vars": "all", "args": "off", "ignoreRestSiblings": false }]
|
||||
"@typescript-eslint/no-unused-vars": ["error", { "vars": "all", "args": "none", "ignoreRestSiblings": false }]
|
||||
}
|
||||
}
|
||||
|
||||
Generated
+1927
-1927
File diff suppressed because it is too large
Load Diff
@@ -3,24 +3,12 @@ export default {
|
||||
'ceil': (v: number) => Math.ceil(v),
|
||||
'divided_by': (v: number, arg: number) => v / arg,
|
||||
'floor': (v: number) => Math.floor(v),
|
||||
'minus': bindFixed((v: number, arg: number) => v - arg),
|
||||
'modulo': bindFixed((v: number, arg: number) => v % arg),
|
||||
'minus': (v: number, arg: number) => v - arg,
|
||||
'modulo': (v: number, arg: number) => v % arg,
|
||||
'round': (v: number, arg: number = 0) => {
|
||||
const amp = Math.pow(10, arg)
|
||||
return Math.round(v * amp) / amp
|
||||
},
|
||||
'plus': bindFixed((v: number, arg: number) => Number(v) + Number(arg)),
|
||||
'plus': (v: number, arg: number) => Number(v) + Number(arg),
|
||||
'times': (v: number, arg: number) => v * arg
|
||||
}
|
||||
|
||||
function getFixed (v: number) {
|
||||
const p = String(v).split('.')
|
||||
return (p.length > 1) ? p[1].length : 0
|
||||
}
|
||||
|
||||
function bindFixed (cb: (v: number, arg: number) => number) {
|
||||
return (l: number, r: number) => {
|
||||
const f = Math.max(getFixed(l), getFixed(r))
|
||||
return cb(l, r).toFixed(f)
|
||||
}
|
||||
}
|
||||
|
||||
+1
-3
@@ -1,7 +1,5 @@
|
||||
import { deprecate } from '../util/deprecate'
|
||||
|
||||
export abstract class Drop {
|
||||
valueOf(): any {
|
||||
valueOf (): any {
|
||||
return undefined
|
||||
}
|
||||
|
||||
|
||||
@@ -64,8 +64,8 @@ export function parseExp (exp: string, scope: Scope): any {
|
||||
}
|
||||
|
||||
if ((match = exp.match(lexical.rangeLine))) {
|
||||
const low = parseValue(match[1], scope)
|
||||
const high = parseValue(match[2], scope)
|
||||
const low = evalValue(match[1], scope)
|
||||
const high = evalValue(match[2], scope)
|
||||
return range(low, high + 1)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
const reported:{[key: string]: boolean} = {}
|
||||
|
||||
export function deprecate(msg: string, issue: number) {
|
||||
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
-15
@@ -96,21 +96,7 @@ export function isObject (value: any): value is object {
|
||||
return value !== null && (type === 'object' || type === 'function')
|
||||
}
|
||||
|
||||
/*
|
||||
* A function to create flexibly-numbered lists of integers,
|
||||
* handy for each and map loops. start, if omitted, defaults to 0; step defaults to 1.
|
||||
* Returns a list of integers from start (inclusive) to stop (exclusive),
|
||||
* incremented (or decremented) by step, exclusive.
|
||||
* Note that ranges that stop before they start are considered to be zero-length instead of
|
||||
* negative — if you'd like a negative range, use a negative step.
|
||||
*/
|
||||
export function range (start: number, stop?: number, step?: number) {
|
||||
if (stop === undefined) {
|
||||
stop = start
|
||||
start = 0
|
||||
}
|
||||
step = step || 1
|
||||
|
||||
export function range (start: number, stop: number, step: number = 1) {
|
||||
const arr: number[] = []
|
||||
for (let i = start; i < stop; i += step) {
|
||||
arr.push(i)
|
||||
|
||||
+2
-3
@@ -6,10 +6,10 @@ use(chaiAsPromised)
|
||||
|
||||
class SettingsDrop extends Liquid.Types.Drop {
|
||||
foo: string = 'FOO'
|
||||
bar() {
|
||||
bar () {
|
||||
return 'BAR'
|
||||
}
|
||||
liquidMethodMissing(key: string) {
|
||||
liquidMethodMissing (key: string) {
|
||||
return key.toUpperCase()
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,6 @@ describe('drop', function () {
|
||||
engine = new Liquid()
|
||||
})
|
||||
it('should support liquidMethodMissing', async function () {
|
||||
let i = 0
|
||||
const src = `{{settings.foo}},{{settings.bar}},{{settings.coo}}`
|
||||
const html = await engine.parseAndRender(src, { settings })
|
||||
return expect(html).to.equal('FOO,BAR,COO')
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { test } from '../../../stub/render'
|
||||
import { expect } from 'chai'
|
||||
import { test, liquid } from '../../../stub/render'
|
||||
|
||||
describe('filters/math', function () {
|
||||
describe('abs', function () {
|
||||
@@ -35,8 +36,10 @@ describe('filters/math', function () {
|
||||
describe('modulo', function () {
|
||||
it('should return "1" for 3,2', () => test('{{ 3 | modulo: 2 }}', '1'))
|
||||
it('should return "3" for 24,7', () => test('{{ 24 | modulo: 7 }}', '3'))
|
||||
it('should return "3.357" for 183.357,12',
|
||||
() => test('{{ 183.357 | modulo: 12 }}', '3.357'))
|
||||
it('should return "3.357" for 183.357,12', async () => {
|
||||
const html = await liquid.parseAndRender('{{ 183.357 | modulo: 12 }}')
|
||||
expect(Number(html)).to.be.closeTo(3.357, 0.001)
|
||||
})
|
||||
it('should convert string', () => test('{{ "24" | modulo: "7" }}', '3'))
|
||||
})
|
||||
describe('plus', function () {
|
||||
|
||||
@@ -7,29 +7,29 @@ describe('drop/drop', function () {
|
||||
|
||||
class CustomDrop extends Liquid.Types.Drop {
|
||||
name: string = 'NAME'
|
||||
getName() {
|
||||
getName () {
|
||||
return 'GETNAME'
|
||||
}
|
||||
}
|
||||
class CustomDropWithMethodMissing extends CustomDrop {
|
||||
liquidMethodMissing(key: string) {
|
||||
liquidMethodMissing (key: string) {
|
||||
return key.toUpperCase()
|
||||
}
|
||||
}
|
||||
it('should call corresponding method', async function () {
|
||||
const html = await liquid.parseAndRender(`{{obj.getName}}`, {obj: new CustomDrop()})
|
||||
const html = await liquid.parseAndRender(`{{obj.getName}}`, { obj: new CustomDrop() })
|
||||
expect(html).to.equal('GETNAME')
|
||||
})
|
||||
it('should read corresponding property', async function () {
|
||||
const html = await liquid.parseAndRender(`{{obj.name}}`, {obj: new CustomDrop()})
|
||||
const html = await liquid.parseAndRender(`{{obj.name}}`, { obj: new CustomDrop() })
|
||||
expect(html).to.equal('NAME')
|
||||
})
|
||||
it('should output empty string if not exist', async function () {
|
||||
const html = await liquid.parseAndRender(`{{obj.foo}}`, {obj: new CustomDrop()})
|
||||
const html = await liquid.parseAndRender(`{{obj.foo}}`, { obj: new CustomDrop() })
|
||||
expect(html).to.equal('')
|
||||
})
|
||||
it('should respect liquidMethodMissing', async function () {
|
||||
const html = await liquid.parseAndRender(`{{obj.foo}}`, {obj: new CustomDropWithMethodMissing()})
|
||||
const html = await liquid.parseAndRender(`{{obj.foo}}`, { obj: new CustomDropWithMethodMissing() })
|
||||
expect(html).to.equal('FOO')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import Liquid from '../../../src/liquid'
|
||||
import * as chai from 'chai'
|
||||
import { mock, restore } from '../../stub/mockfs'
|
||||
import * as chaiAsPromised from 'chai-as-promised'
|
||||
|
||||
const expect = chai.expect
|
||||
chai.use(chaiAsPromised)
|
||||
|
||||
describe('Liquid', function () {
|
||||
describe('#plugin()', function () {
|
||||
@@ -34,6 +36,12 @@ describe('Liquid', function () {
|
||||
const html = await engine.parseAndRender(tpl)
|
||||
expect(html).to.equal('Welcome(to]Liquid')
|
||||
})
|
||||
it('should support for-in with variable', async function () {
|
||||
const src = '{% assign total = 3 | minus: 1 %}' +
|
||||
'{% for i in (1..total) %}{{ i }}{% endfor %}'
|
||||
const html = await engine.parseAndRender(src, {})
|
||||
return expect(html).to.equal('12')
|
||||
})
|
||||
})
|
||||
describe('#express()', function () {
|
||||
const liquid = new Liquid({ root: '/root' })
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
import Liquid from '../../src/liquid'
|
||||
import { expect } from 'chai'
|
||||
|
||||
const liquid = new Liquid()
|
||||
export const liquid = new Liquid()
|
||||
|
||||
export const ctx = {
|
||||
date: new Date(),
|
||||
|
||||
@@ -81,9 +81,6 @@ describe('util/underscore', function () {
|
||||
it('should return a range of integers', function () {
|
||||
expect(_.range(3, 5)).to.deep.equal([3, 4])
|
||||
})
|
||||
it('should start from 0 if begin omitted', function () {
|
||||
expect(_.range(3)).to.deep.equal([0, 1, 2])
|
||||
})
|
||||
})
|
||||
describe('.isObject()', function () {
|
||||
it('should return true for function', function () {
|
||||
|
||||
Reference in New Issue
Block a user