UNPKG

handlebars-helpers-v2

Version:

Essential Handlebars helpers in TypeScript. A modernized collection of 8 core helper categories with TypeScript support and ESM compatibility.

93 lines 3.2 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const util = __importStar(require("handlebars-utils")); const utils = __importStar(require("./utils")); const helpers = {}; /** * Inline, subexpression, or block helper that returns true (or the block) * if the given collection is empty, or false (or the inverse block, if * supplied) if the colleciton is not empty. * * ```handlebars * <!-- array: [] --> * {{#isEmpty array}}AAA{{else}}BBB{{/isEmpty}} * <!-- results in: 'AAA' --> * * <!-- array: [] --> * {{isEmpty array}} * <!-- results in: true --> * ``` * @param {Object} `collection` * @param {Object} `options` * @return {String} * @block * @api public */ helpers.isEmpty = function (collection, options) { if (!util.isOptions(options)) { options = collection; return util.fn(true, this, options); } if (Array.isArray(collection) && !collection.length) { return util.fn(true, this, options); } var keys = Object.keys(collection); var isEmpty = typeof collection === 'object' && !keys.length; return util.value(isEmpty, this, options); }; /** * Block helper that iterates over an array or object. If * an array is given, `.forEach` is called, or if an object * is given, `.forOwn` is called, otherwise the inverse block * is returned. * * @param {Object|Array} `collection` The collection to iterate over * @param {Object} `options` * @return {String} * @block * @api public */ helpers.iterate = function (collection, options) { if (Array.isArray(collection)) { return utils.forEach.apply(null, arguments); } if (util.isObject(collection)) { return utils.forOwn.apply(null, arguments); } return options.inverse(this); }; exports.default = helpers; //# sourceMappingURL=collection.js.map