UNPKG

@mendix/pluggable-widgets-tools

Version:
41 lines (40 loc) 1.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractProperties = extractProperties; exports.extractSystemProperties = extractSystemProperties; exports.capitalizeFirstLetter = capitalizeFirstLetter; exports.commasAnd = commasAnd; exports.groupBy = groupBy; function extractProperties(props) { return extractProps(props, p => { var _a; return (_a = p.property) !== null && _a !== void 0 ? _a : []; }); } function extractSystemProperties(props) { return extractProps(props, p => { var _a; return (_a = p.systemProperty) !== null && _a !== void 0 ? _a : []; }); } function extractProps(props, extractor) { return props.propertyGroup ? props.propertyGroup.map(pg => extractProps(pg, extractor)).reduce((a, e) => a.concat(e), []) : extractor(props); } function capitalizeFirstLetter(text) { return text.charAt(0).toUpperCase() + text.slice(1); } function commasAnd(arr) { return arr.slice(0, -1).join(", ") + (arr.length > 1 ? " and " : "") + arr[arr.length - 1]; } /** * Factory method for creating an `Array.reduce()` callback for partitioning the array into groups. * @example * ```js * [1, 4, 3, 13].reduce(groupBy(x => x % 2 ? "odd" : "even")).even // [ 4 ] * ``` * @param groupSelector Callback that maps array members to their group * @returns Callback for Array.reduce() */ function groupBy(groupSelector) { return function reducer(reduction, item) { var _a; const group = groupSelector(item); return Object.assign(Object.assign({}, reduction), { [group]: [...((_a = reduction[group]) !== null && _a !== void 0 ? _a : []), item] }); }; }