twing
Version:
First-class Twig engine for Node.js
69 lines (68 loc) • 2.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isEmptySynchronously = exports.isEmpty = void 0;
const iterator_to_array_1 = require("../../../helpers/iterator-to-array");
const isPlainObject = require('is-plain-object');
/**
* Checks if a variable is empty.
*
* <pre>
* {# evaluates to true if the foo variable is null, false, or the empty string #}
* {% if foo is empty %}
* {# ... #}
* {% endif %}
* </pre>
*
* @param executionContext
* @param value A variable
*
* @returns {boolean} true if the value is empty, false otherwise
*/
const isEmpty = (executionContext, value) => {
if (value === null || value === undefined) {
return Promise.resolve(true);
}
if (typeof value === 'string') {
return Promise.resolve(value.length < 1);
}
if (typeof value[Symbol.iterator] === 'function') {
return Promise.resolve(value[Symbol.iterator]().next().done === true);
}
if (isPlainObject(value)) {
if (value.hasOwnProperty('toString') && typeof value.toString === 'function') {
return (0, exports.isEmpty)(executionContext, value.toString());
}
else {
return Promise.resolve((0, iterator_to_array_1.iteratorToArray)(value).length < 1);
}
}
if (typeof value === 'object' && value.toString && typeof value.toString === 'function') {
return (0, exports.isEmpty)(executionContext, value.toString());
}
return Promise.resolve(value === false);
};
exports.isEmpty = isEmpty;
const isEmptySynchronously = (executionContext, value) => {
if (value === null || value === undefined) {
return true;
}
if (typeof value === 'string') {
return value.length < 1;
}
if (typeof value[Symbol.iterator] === 'function') {
return value[Symbol.iterator]().next().done === true;
}
if (isPlainObject(value)) {
if (value.hasOwnProperty('toString') && typeof value.toString === 'function') {
return (0, exports.isEmptySynchronously)(executionContext, value.toString());
}
else {
return (0, iterator_to_array_1.iteratorToArray)(value).length < 1;
}
}
if (typeof value === 'object' && value.toString && typeof value.toString === 'function') {
return (0, exports.isEmptySynchronously)(executionContext, value.toString());
}
return value === false;
};
exports.isEmptySynchronously = isEmptySynchronously;