UNPKG

locutus

Version:

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

33 lines (32 loc) 1.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.array_diff_ukey = array_diff_ukey; const _callbackResolver_ts_1 = require("../_helpers/_callbackResolver.js"); const _phpTypes_ts_1 = require("../_helpers/_phpTypes.js"); function array_diff_ukey(arr1, ...arraysAndCallback) { // discuss at: https://locutus.io/php/array_diff_ukey/ // original by: Brett Zamir (https://brett-zamir.me) // example 1: var $array1 = {blue: 1, red: 2, green: 3, purple: 4} // example 1: var $array2 = {green: 5, blue: 6, yellow: 7, cyan: 8} // example 1: array_diff_ukey($array1, $array2, function (key1, key2){ return (key1 === key2 ? 0 : (key1 > key2 ? 1 : -1)); }) // returns 1: {red: 2, purple: 4} const retArr = {}; const callback = arraysAndCallback.at(-1); if (typeof callback === 'undefined' || !(0, _phpTypes_ts_1.isPhpCallableDescriptor)(callback)) { throw new Error('array_diff_ukey(): Invalid callback'); } const arrays = arraysAndCallback.slice(0, -1).map((value) => (0, _phpTypes_ts_1.toPhpArrayObject)(value)); const keyComparator = (0, _callbackResolver_ts_1.resolveNumericComparator)(callback, 'array_diff_ukey(): Invalid callback'); arr1keys: for (const [k1, arr1Value] of (0, _phpTypes_ts_1.entriesOfPhpAssoc)(arr1)) { for (const arr of arrays) { for (const [k] of (0, _phpTypes_ts_1.entriesOfPhpAssoc)(arr)) { if (keyComparator(k, k1) === 0) { // If it reaches here, it was found in at least one array, so try next value continue arr1keys; } } } retArr[k1] = arr1Value; } return retArr; }