UNPKG

locutus

Version:

Locutus other languages' standard libraries to JavaScript for fun and educational purposes

31 lines (30 loc) 1.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.filter_map = filter_map; function filter_map(values, callback) { // discuss at: https://locutus.io/ruby/Array/filter_map/ // parity verified: Ruby 3.3 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Maps each value and keeps only callback results that are neither false nor nil. // example 1: filter_map([1, 2, 3, 4], (value) => (value % 2 === 0 ? value : null)) // returns 1: [2, 4] // example 2: filter_map(['a', 'bb', 'ccc'], (value) => (value.length > 1 ? value + value : null)) // returns 2: ['bbbb', 'cccccc'] // example 3: filter_map([], (value) => value) // returns 3: [] if (typeof callback !== 'function') { throw new TypeError('filter_map(): callback must be a function'); } if (!Array.isArray(values)) { return []; } const out = []; for (let index = 0; index < values.length; index += 1) { const value = values[index]; const mapped = callback(value, index, values); if (mapped !== false && mapped !== null && mapped !== undefined) { out.push(mapped); } } return out; }