mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-29 15:15:23 -07:00
Add isPlainObject to 'util/underscore.js'
This commit is contained in:
@@ -94,6 +94,15 @@ function isObject (value) {
|
|||||||
return value !== null && typeof value === 'object'
|
return value !== null && typeof value === 'object'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Checks if value's prototype is Object or undefined.
|
||||||
|
* @param {any} value The value to check.
|
||||||
|
* @return {Boolean} Returns true if value is a plain object, else false.
|
||||||
|
*/
|
||||||
|
function isPlainObject(value) {
|
||||||
|
return value && !value.constructor || value.constructor === Object
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* A function to create flexibly-numbered lists of integers,
|
* 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.
|
* handy for each and map loops. start, if omitted, defaults to 0; step defaults to 1.
|
||||||
@@ -119,6 +128,7 @@ function range (start, stop, step) {
|
|||||||
exports.isString = isString
|
exports.isString = isString
|
||||||
exports.isArray = isArray
|
exports.isArray = isArray
|
||||||
exports.isObject = isObject
|
exports.isObject = isObject
|
||||||
|
exports.isPlainObject = isPlainObject
|
||||||
exports.isError = isError
|
exports.isError = isError
|
||||||
|
|
||||||
exports.range = range
|
exports.range = range
|
||||||
|
|||||||
+3
-17
@@ -1,6 +1,7 @@
|
|||||||
const Liquid = require('..')
|
const Liquid = require('..')
|
||||||
const lexical = Liquid.lexical
|
const lexical = Liquid.lexical
|
||||||
const mapSeries = require('../src/util/promise.js').mapSeries
|
const mapSeries = require('../src/util/promise.js').mapSeries
|
||||||
|
const isPlainObject = require('../src/util/underscore.js').isPlainObject
|
||||||
const RenderBreakError = Liquid.Types.RenderBreakError
|
const RenderBreakError = Liquid.Types.RenderBreakError
|
||||||
const assert = require('../src/util/assert.js')
|
const assert = require('../src/util/assert.js')
|
||||||
const re = new RegExp(`^(${lexical.identifier.source})\\s+in\\s+` +
|
const re = new RegExp(`^(${lexical.identifier.source})\\s+in\\s+` +
|
||||||
@@ -38,10 +39,10 @@ module.exports = function (liquid) {
|
|||||||
render: function (scope, hash) {
|
render: function (scope, hash) {
|
||||||
var collection = Liquid.evalExp(this.collection, scope)
|
var collection = Liquid.evalExp(this.collection, scope)
|
||||||
|
|
||||||
if (isObject(collection)) {
|
if (isPlainObject(collection)) {
|
||||||
collection = Object.keys(collection)
|
collection = Object.keys(collection)
|
||||||
}
|
}
|
||||||
if (isEmpty(collection)) {
|
if (!Array.isArray(collection) || !collection.length) {
|
||||||
return liquid.renderer.renderTemplates(this.elseTemplates, scope)
|
return liquid.renderer.renderTemplates(this.elseTemplates, scope)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,18 +93,3 @@ module.exports = function (liquid) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function isObject(collection) {
|
|
||||||
if (collection) {
|
|
||||||
const ctr = collection.constructor
|
|
||||||
return ctr === Object || ctr == null
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
function isEmpty(collection) {
|
|
||||||
if (Array.isArray(collection)) {
|
|
||||||
return !collection.length
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user