mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-17 05:10:40 -07:00
chore(TypeScript): refactor objects into classes
fix: `Nil`(null, undefined) now renders as empty string change: `parser.parseValue()` renamed to `parser.parseOutput` change: registered tags/filters become static and shared across different liquid instances
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
||||
import { AssertionError } from './error'
|
||||
|
||||
export default function (predicate: any, message: string) {
|
||||
export default function (predicate: any, message?: string) {
|
||||
if (!predicate) {
|
||||
message = message || `expect ${predicate} to be true`
|
||||
throw new AssertionError(message)
|
||||
|
||||
+5
-9
@@ -1,4 +1,5 @@
|
||||
import * as _ from './underscore'
|
||||
import Token from 'src/parser/token'
|
||||
|
||||
function captureStack () {
|
||||
if (Error.captureStackTrace) {
|
||||
@@ -13,8 +14,8 @@ abstract class LiquidError {
|
||||
message: string
|
||||
name: string
|
||||
stack: string
|
||||
token: any
|
||||
originalError: any
|
||||
token: Token
|
||||
originalError: Error
|
||||
constructor(err, token) {
|
||||
this.input = token.input
|
||||
this.line = token.line
|
||||
@@ -66,6 +67,7 @@ RenderError.prototype.constructor = RenderError
|
||||
|
||||
export class RenderBreakError {
|
||||
message: string
|
||||
resolvedHTML: string
|
||||
constructor (message) {
|
||||
captureStack.call(this)
|
||||
this.message = message + ''
|
||||
@@ -93,7 +95,7 @@ function mkContext (input, targetLine) {
|
||||
.range(begin, end + 1)
|
||||
.map(lineNumber => {
|
||||
const indicator = (lineNumber === targetLine) ? '>> ' : ' '
|
||||
const num = padStart(String(end).length, lineNumber)
|
||||
const num = _.padStart(String(lineNumber), String(end).length)
|
||||
const text = lines[lineNumber - 1]
|
||||
return `${indicator}${num}| ${text}`
|
||||
})
|
||||
@@ -112,9 +114,3 @@ function mkMessage (msg, token) {
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
function padStart (length, str) {
|
||||
str = String(str)
|
||||
const blank = Array(length - str.length).join(' ')
|
||||
return blank + str
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
/*
|
||||
* Call functions in serial until someone resolved.
|
||||
* @param {Array} iterable the array to iterate with.
|
||||
* @param {Array} iteratee returns a new promise.
|
||||
* @param iterable the array to iterate with.
|
||||
* @param iteratee returns a new promise.
|
||||
* The iteratee is invoked with three arguments: (value, index, iterable).
|
||||
*/
|
||||
export function anySeries (iterable, iteratee) {
|
||||
let ret = Promise.reject(new Error('init'))
|
||||
let ret: Promise<any> = Promise.reject(new Error('init'))
|
||||
iterable.forEach(function (item, idx) {
|
||||
ret = ret.catch(e => iteratee(item, idx, iterable))
|
||||
})
|
||||
@@ -19,7 +19,7 @@ export function anySeries (iterable, iteratee) {
|
||||
* The iteratee is invoked with three arguments: (value, index, iterable).
|
||||
*/
|
||||
export function mapSeries (iterable, iteratee) {
|
||||
let ret = Promise.resolve('init')
|
||||
let ret: Promise<any> = Promise.resolve('init')
|
||||
const result = []
|
||||
iterable.forEach(function (item, idx) {
|
||||
ret = ret
|
||||
@@ -1,3 +1,5 @@
|
||||
import { padStart } from './underscore'
|
||||
|
||||
const monthNames = [
|
||||
'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
|
||||
'September', 'October', 'November', 'December'
|
||||
@@ -41,7 +43,7 @@ const _date = {
|
||||
// Find the first startDay of the year
|
||||
const jan1 = new Date(d.getFullYear(), 0, 1)
|
||||
const then = (7 - jan1.getDay() + startDay)
|
||||
return _number.pad(Math.floor((now - then) / 7) + 1, 2)
|
||||
return padStart(String(Math.floor((now - then) / 7) + 1), 2, '0')
|
||||
},
|
||||
|
||||
isLeapYear: function (d) {
|
||||
@@ -60,20 +62,6 @@ const _date = {
|
||||
}
|
||||
}
|
||||
|
||||
const _number = {
|
||||
pad: function (value, size, ch) {
|
||||
if (!ch) ch = '0'
|
||||
let result = value.toString()
|
||||
let pad = size - result.length
|
||||
|
||||
while (pad-- > 0) {
|
||||
result = ch + result
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
const formatCodes = {
|
||||
a: function (d) {
|
||||
return dayNamesShort[d.getDay()]
|
||||
@@ -94,34 +82,34 @@ const formatCodes = {
|
||||
return _date.century(d)
|
||||
},
|
||||
d: function (d) {
|
||||
return _number.pad(d.getDate(), 2)
|
||||
return padStart(d.getDate(), 2, '0')
|
||||
},
|
||||
e: function (d) {
|
||||
return _number.pad(d.getDate(), 2, ' ')
|
||||
return padStart(d.getDate(), 2)
|
||||
},
|
||||
H: function (d) {
|
||||
return _number.pad(d.getHours(), 2)
|
||||
return padStart(d.getHours(), 2, '0')
|
||||
},
|
||||
I: function (d) {
|
||||
return _number.pad(d.getHours() % 12 || 12, 2)
|
||||
return padStart(String(d.getHours() % 12 || 12), 2, '0')
|
||||
},
|
||||
j: function (d) {
|
||||
return _number.pad(_date.getDayOfYear(d), 3)
|
||||
return padStart(_date.getDayOfYear(d), 3, '0')
|
||||
},
|
||||
k: function (d) {
|
||||
return _number.pad(d.getHours(), 2, ' ')
|
||||
return padStart(d.getHours(), 2)
|
||||
},
|
||||
l: function (d) {
|
||||
return _number.pad(d.getHours() % 12 || 12, 2, ' ')
|
||||
return padStart(String(d.getHours() % 12 || 12), 2)
|
||||
},
|
||||
L: function (d) {
|
||||
return _number.pad(d.getMilliseconds(), 3)
|
||||
return padStart(d.getMilliseconds(), 3, '0')
|
||||
},
|
||||
m: function (d) {
|
||||
return _number.pad(d.getMonth() + 1, 2)
|
||||
return padStart(d.getMonth() + 1, 2, '0')
|
||||
},
|
||||
M: function (d) {
|
||||
return _number.pad(d.getMinutes(), 2)
|
||||
return padStart(d.getMinutes(), 2, '0')
|
||||
},
|
||||
p: function (d) {
|
||||
return (d.getHours() < 12 ? 'AM' : 'PM')
|
||||
@@ -136,7 +124,7 @@ const formatCodes = {
|
||||
return Math.round(d.valueOf() / 1000)
|
||||
},
|
||||
S: function (d) {
|
||||
return _number.pad(d.getSeconds(), 2)
|
||||
return padStart(d.getSeconds(), 2, '0')
|
||||
},
|
||||
u: function (d) {
|
||||
return d.getDay() || 7
|
||||
@@ -164,14 +152,14 @@ const formatCodes = {
|
||||
},
|
||||
z: function (d) {
|
||||
const tz = d.getTimezoneOffset() / 60 * 100
|
||||
return (tz > 0 ? '-' : '+') + _number.pad(Math.abs(tz), 4)
|
||||
return (tz > 0 ? '-' : '+') + padStart(String(Math.abs(tz)), 4, '0')
|
||||
},
|
||||
'%': function () {
|
||||
return '%'
|
||||
}
|
||||
}
|
||||
formatCodes.h = formatCodes.b
|
||||
formatCodes.N = formatCodes.L
|
||||
};
|
||||
(formatCodes as any).h = formatCodes.b;
|
||||
(formatCodes as any).N = formatCodes.L;
|
||||
|
||||
export default function (d, format) {
|
||||
let output = ''
|
||||
@@ -25,7 +25,7 @@ export function promisify (fn) {
|
||||
}
|
||||
|
||||
export function stringify (value) {
|
||||
if (isNil(value)) return String(value)
|
||||
if (isNil(value)) return ''
|
||||
if (isFunction(value.to_liquid)) return stringify(value.to_liquid())
|
||||
if (isFunction(value.toLiquid)) return stringify(value.toLiquid())
|
||||
if (isFunction(value.to_s)) return value.to_s()
|
||||
@@ -142,7 +142,7 @@ export function isObject (value) {
|
||||
* 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) {
|
||||
export function range (start: number, stop?: number, step?: number) {
|
||||
if (arguments.length === 1) {
|
||||
stop = start
|
||||
start = 0
|
||||
@@ -155,3 +155,10 @@ export function range (start: number, stop: number, step?: number) {
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
export function padStart(str: any, length: number, ch: string = ' ') {
|
||||
str = String(str)
|
||||
let n = length - str.length
|
||||
while(n-- > 0) str = ch + str
|
||||
return str
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user