mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-20 15:00:42 -07:00
feat: pop/shift/unshift filters from Jekyll
This commit is contained in:
+23
-9
@@ -11,7 +11,7 @@ export const reverse = argumentsToValue((v: any[]) => [...toArray(v)].reverse())
|
||||
|
||||
export function * sort<T> (this: FilterImpl, arr: T[], property?: string): IterableIterator<unknown> {
|
||||
const values: [T, string | number][] = []
|
||||
for (const item of toArray(toValue(arr))) {
|
||||
for (const item of toArray(arr)) {
|
||||
values.push([
|
||||
item,
|
||||
property ? yield this.context._getFromScope(item, stringify(property).split('.'), false) : item
|
||||
@@ -25,7 +25,6 @@ export function * sort<T> (this: FilterImpl, arr: T[], property?: string): Itera
|
||||
}
|
||||
|
||||
export function sort_natural<T> (input: T[], property?: string) {
|
||||
input = toValue(input)
|
||||
const propertyString = stringify(property)
|
||||
const compare = property === undefined
|
||||
? caseInsensitiveCompare
|
||||
@@ -37,7 +36,7 @@ export const size = (v: string | any[]) => (v && v.length) || 0
|
||||
|
||||
export function * map (this: FilterImpl, arr: Scope[], property: string): IterableIterator<unknown> {
|
||||
const results = []
|
||||
for (const item of toArray(toValue(arr))) {
|
||||
for (const item of toArray(arr)) {
|
||||
results.push(yield this.context._getFromScope(item, stringify(property), false))
|
||||
}
|
||||
return results
|
||||
@@ -45,7 +44,7 @@ export function * map (this: FilterImpl, arr: Scope[], property: string): Iterab
|
||||
|
||||
export function * sum (this: FilterImpl, arr: Scope[], property?: string): IterableIterator<unknown> {
|
||||
let sum = 0
|
||||
for (const item of toArray(toValue(arr))) {
|
||||
for (const item of toArray(arr)) {
|
||||
const data = Number(property ? yield this.context._getFromScope(item, stringify(property), false) : item)
|
||||
sum += Number.isNaN(data) ? 0 : data
|
||||
}
|
||||
@@ -53,20 +52,35 @@ export function * sum (this: FilterImpl, arr: Scope[], property?: string): Itera
|
||||
}
|
||||
|
||||
export function compact<T> (this: FilterImpl, arr: T[]) {
|
||||
arr = toValue(arr)
|
||||
return toArray(arr).filter(x => !isNil(toValue(x)))
|
||||
}
|
||||
|
||||
export function concat<T1, T2> (v: T1[], arg: T2[] = []): (T1 | T2)[] {
|
||||
v = toValue(v)
|
||||
arg = toArray(arg).map(v => toValue(v))
|
||||
return toArray(v).concat(arg)
|
||||
return toArray(v).concat(toArray(arg))
|
||||
}
|
||||
|
||||
export function push<T> (v: T[], arg: T): T[] {
|
||||
return concat(v, [arg])
|
||||
}
|
||||
|
||||
export function unshift<T> (v: T[], arg: T): T[] {
|
||||
const clone = [...toArray(v)]
|
||||
clone.unshift(arg)
|
||||
return clone
|
||||
}
|
||||
|
||||
export function pop<T> (v: T[]): T[] {
|
||||
const clone = [...toArray(v)]
|
||||
clone.pop()
|
||||
return clone
|
||||
}
|
||||
|
||||
export function shift<T> (v: T[]): T[] {
|
||||
const clone = [...toArray(v)]
|
||||
clone.shift()
|
||||
return clone
|
||||
}
|
||||
|
||||
export function slice<T> (v: T[] | string, begin: number, length = 1): T[] | string {
|
||||
v = toValue(v)
|
||||
if (isNil(v)) return []
|
||||
@@ -77,7 +91,7 @@ export function slice<T> (v: T[] | string, begin: number, length = 1): T[] | str
|
||||
|
||||
export function * where<T extends object> (this: FilterImpl, arr: T[], property: string, expected?: any): IterableIterator<unknown> {
|
||||
const values: unknown[] = []
|
||||
arr = toArray(toValue(arr))
|
||||
arr = toArray(arr)
|
||||
for (const item of arr) {
|
||||
values.push(yield this.context._getFromScope(item, stringify(property).split('.'), false))
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
import { Hash, ValueToken, Liquid, Tag, evalToken, Emitter, TagToken, TopLevelToken, Context, Template, ParseStream } from '..'
|
||||
import { toEnumerable } from '../util/collection'
|
||||
import { toEnumerable } from '../util'
|
||||
import { ForloopDrop } from '../drop/forloop-drop'
|
||||
|
||||
const MODIFIERS = ['offset', 'limit', 'reversed']
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { toEnumerable } from '../util/collection'
|
||||
import { toEnumerable } from '../util'
|
||||
import { ValueToken, Liquid, Tag, evalToken, Emitter, Hash, TagToken, TopLevelToken, Context, Template, ParseStream } from '..'
|
||||
import { TablerowloopDrop } from '../drop/tablerowloop-drop'
|
||||
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
import { isNil, isString, isObject, isArray, isIterable, toValue } from './underscore'
|
||||
|
||||
export function toEnumerable<T = unknown> (val: any): T[] {
|
||||
val = toValue(val)
|
||||
if (isArray(val)) return val
|
||||
if (isString(val) && val.length > 0) return [val] as unknown as T[]
|
||||
if (isIterable(val)) return Array.from(val)
|
||||
if (isObject(val)) return Object.keys(val).map((key) => [key, val[key]]) as unknown as T[]
|
||||
return []
|
||||
}
|
||||
|
||||
export function toArray (val: any) {
|
||||
if (isNil(val)) return []
|
||||
if (isArray(val)) return val
|
||||
return [ val ]
|
||||
}
|
||||
@@ -6,7 +6,6 @@ export * from './underscore'
|
||||
export * from './operator-trie'
|
||||
export * from './type-guards'
|
||||
export * from './async'
|
||||
export * from './collection'
|
||||
export * from './strftime'
|
||||
export * from './liquid-date'
|
||||
export * from './timezone-date'
|
||||
|
||||
@@ -46,6 +46,22 @@ export function stringify (value: any): string {
|
||||
return String(value)
|
||||
}
|
||||
|
||||
export function toEnumerable<T = unknown> (val: any): T[] {
|
||||
val = toValue(val)
|
||||
if (isArray(val)) return val
|
||||
if (isString(val) && val.length > 0) return [val] as unknown as T[]
|
||||
if (isIterable(val)) return Array.from(val)
|
||||
if (isObject(val)) return Object.keys(val).map((key) => [key, val[key]]) as unknown as T[]
|
||||
return []
|
||||
}
|
||||
|
||||
export function toArray (val: any) {
|
||||
val = toValue(val)
|
||||
if (isNil(val)) return []
|
||||
if (isArray(val)) return val
|
||||
return [ val ]
|
||||
}
|
||||
|
||||
export function toValue (value: any): any {
|
||||
return (value instanceof Drop && isFunction(value.valueOf)) ? value.valueOf() : value
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user