UNPKG

locutus

Version:

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

44 lines (43 loc) 1.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.array_map = array_map; const _callbackResolver_ts_1 = require("../_helpers/_callbackResolver.js"); function array_map(callback, ...inputArrays) { // discuss at: https://locutus.io/php/array_map/ // original by: Andrea Giammarchi (https://webreflection.blogspot.com) // improved by: Kevin van Zonneveld (https://kvz.io) // improved by: Brett Zamir (https://brett-zamir.me) // input by: thekid // note 1: If the callback is a string (or object, if an array is supplied), // note 1: it can only work if the function name is in the global context // example 1: array_map( function (a){return (a * a * a)}, [1, 2, 3, 4, 5] ) // returns 1: [ 1, 8, 27, 64, 125 ] const argc = inputArrays.length + 1; const itemCount = inputArrays[0]?.length ?? 0; const resolved = callback === null || typeof callback === 'undefined' ? null : (0, _callbackResolver_ts_1.resolvePhpCallable)(callback, { invalidMessage: 'array_map(): Invalid callback', missingScopeMessage: (scopeName) => 'Object not found: ' + scopeName, }); if (resolved) { const mapped = []; for (let itemIndex = 0; itemIndex < itemCount; itemIndex++) { const args = []; for (let arrayIndex = 0; arrayIndex < argc - 1; arrayIndex++) { args.push(inputArrays[arrayIndex]?.[itemIndex]); } mapped[itemIndex] = resolved.fn.apply(resolved.scope, args); } return mapped; } const mapped = []; for (let itemIndex = 0; itemIndex < itemCount; itemIndex++) { const args = []; for (let arrayIndex = 0; arrayIndex < argc - 1; arrayIndex++) { args.push(inputArrays[arrayIndex]?.[itemIndex]); } mapped[itemIndex] = args; } return mapped; }