|
| 1 | +// app/lib/utils/arrays.js |
| 2 | + |
| 3 | +const _ = require('lodash') |
| 4 | + |
| 5 | +/** |
| 6 | + * Find an object by ID in an array |
| 7 | + * |
| 8 | + * @param {Array} array - Array to search |
| 9 | + * @param {string} id - ID to find |
| 10 | + * @returns {object} Found object or undefined |
| 11 | + */ |
| 12 | +const findById = (array, id) => { |
| 13 | + if (!array || !Array.isArray(array)) return undefined |
| 14 | + return array.find((item) => item.id === id) |
| 15 | +} |
| 16 | + |
| 17 | +const push = (array, item) => { |
| 18 | + const newArray = [...array] |
| 19 | + newArray.push(_.cloneDeep(item)) // clone needed to stop this mutating original |
| 20 | + return newArray |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Check if an array includes a value |
| 25 | + * |
| 26 | + * @param {Array} array - Array to check |
| 27 | + * @param {*} value - Value to look for |
| 28 | + * @returns {boolean} True if array includes value, false otherwise |
| 29 | + */ |
| 30 | +const includes = (array, value) => { |
| 31 | + if (!array || !Array.isArray(array)) return false |
| 32 | + return array.includes(value) |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Find first array item where the specified key matches the value |
| 37 | + * |
| 38 | + * @param {Array} array - Array to search |
| 39 | + * @param {string} key - Object key to match against |
| 40 | + * @param {any} value - Value to find |
| 41 | + * @returns {any} First matching item or undefined |
| 42 | + * @example |
| 43 | + * const users = [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}] |
| 44 | + * find(users, 'name', 'Bob') // Returns {id: 2, name: 'Bob'} |
| 45 | + */ |
| 46 | +const find = (array, key, value) => { |
| 47 | + if (!array || !Array.isArray(array)) return undefined |
| 48 | + return array.find((item) => item[key] === value) |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Remove empty items from arrays or strings |
| 53 | + * |
| 54 | + * @param {Array|string} items - Items to filter |
| 55 | + * @returns {Array|string|undefined} Filtered items or undefined if empty |
| 56 | + */ |
| 57 | +const removeEmpty = (items) => { |
| 58 | + if (!items) return |
| 59 | + |
| 60 | + if (_.isString(items)) { |
| 61 | + return items.trim() || undefined |
| 62 | + } |
| 63 | + |
| 64 | + if (_.isArray(items)) { |
| 65 | + const filtered = items.filter((item) => { |
| 66 | + // Filter out falsy values and empty strings |
| 67 | + if (!item || item === '') return false |
| 68 | + |
| 69 | + // Filter out empty objects |
| 70 | + if ( |
| 71 | + _.isObject(item) && |
| 72 | + !_.isArray(item) && |
| 73 | + Object.keys(item).length === 0 |
| 74 | + ) |
| 75 | + return false |
| 76 | + |
| 77 | + // Filter out empty arrays |
| 78 | + if (_.isArray(item) && item.length === 0) return false |
| 79 | + |
| 80 | + return true |
| 81 | + }) |
| 82 | + return filtered.length ? filtered : undefined |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Filter array to items where the specified property matches one of the comparison values |
| 88 | + * |
| 89 | + * @param {Array} array - Array to filter |
| 90 | + * @param {string} key - Object property path to match against (supports dot notation) |
| 91 | + * @param {*|Array} compare - Value or array of values to match |
| 92 | + * @returns {Array} Filtered array containing only matching items |
| 93 | + * @example |
| 94 | + * where([{type: 'dog'}, {type: 'cat'}], 'type', 'dog') // Returns [{type: 'dog'}] |
| 95 | + * where(users, 'address.postcode', ['OX1', 'OX2']) // Returns users with matching postcodes |
| 96 | + */ |
| 97 | +const where = (array, key, compare) => { |
| 98 | + if (!array || !Array.isArray(array)) return [] |
| 99 | + |
| 100 | + // Force comparison value to array |
| 101 | + const compareValues = Array.isArray(compare) ? compare : [compare] |
| 102 | + |
| 103 | + return array.filter((item) => { |
| 104 | + const value = _.get(item, key) |
| 105 | + return compareValues.includes(value) |
| 106 | + }) |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Filter array to remove items where the specified property matches one of the comparison values |
| 111 | + * |
| 112 | + * @param {Array} array - Array to filter |
| 113 | + * @param {string} key - Object property path to match against (supports dot notation) |
| 114 | + * @param {*|Array} compare - Value or array of values to exclude |
| 115 | + * @returns {Array} Filtered array with matching items removed |
| 116 | + * @example |
| 117 | + * removeWhere([{type: 'dog'}, {type: 'cat'}], 'type', 'dog') // Returns [{type: 'cat'}] |
| 118 | + * removeWhere(users, 'status', ['inactive', 'suspended']) // Returns only active users |
| 119 | + */ |
| 120 | +const removeWhere = (array, key, compare) => { |
| 121 | + if (!array || !Array.isArray(array)) return [] |
| 122 | + |
| 123 | + // Force comparison value to array |
| 124 | + const compareValues = Array.isArray(compare) ? compare : [compare] |
| 125 | + |
| 126 | + return array.filter((item) => { |
| 127 | + const value = _.get(item, key) |
| 128 | + return !compareValues.includes(value) |
| 129 | + }) |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * Apply a filter to each element in an array |
| 134 | + * |
| 135 | + * @param {Array} array - Array to map over |
| 136 | + * @param {string} filterName - Name of the filter to apply to each element |
| 137 | + * @returns {Array} New array with filter applied to each element |
| 138 | + */ |
| 139 | +const map = function (array, filterName) { |
| 140 | + if (!array || !Array.isArray(array)) return [] |
| 141 | + |
| 142 | + // In Nunjucks filter context, 'this' gives us access to the environment |
| 143 | + // and we can access other filters through the environment |
| 144 | + const env = this.env |
| 145 | + |
| 146 | + if (!env || !env.filters || !env.filters[filterName]) { |
| 147 | + console.warn(`Filter '${filterName}' not found`) |
| 148 | + return array |
| 149 | + } |
| 150 | + |
| 151 | + const filterFunction = env.filters[filterName] |
| 152 | + |
| 153 | + return array.map((item) => filterFunction.call(this, item)) |
| 154 | +} |
| 155 | + |
| 156 | +/** |
| 157 | + * Check if a value is an array |
| 158 | + * |
| 159 | + * @param {*} value - Value to check |
| 160 | + * @returns {boolean} True if value is an array, false otherwise |
| 161 | + * @example |
| 162 | + * isArray([1, 2, 3]) // Returns true |
| 163 | + * isArray('hello') // Returns false |
| 164 | + * isArray(null) // Returns false |
| 165 | + */ |
| 166 | +const isArray = (value) => { |
| 167 | + return Array.isArray(value) |
| 168 | +} |
| 169 | + |
| 170 | +module.exports = { |
| 171 | + push, |
| 172 | + includes, |
| 173 | + find, |
| 174 | + removeEmpty, |
| 175 | + findById, |
| 176 | + where, |
| 177 | + removeWhere, |
| 178 | + map, |
| 179 | + isArray |
| 180 | +} |
0 commit comments