locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
30 lines (29 loc) • 1.63 kB
JavaScript
import { resolveNumericComparator } from "../_helpers/_callbackResolver.js";
import { entriesOfPhpAssoc, isPhpCallableDescriptor, toPhpArrayObject, } from "../_helpers/_phpTypes.js";
export function array_diff_uassoc(arr1, ...arraysAndCallback) {
// discuss at: https://locutus.io/php/array_diff_uassoc/
// original by: Brett Zamir (https://brett-zamir.me)
// example 1: var $array1 = {a: 'green', b: 'brown', c: 'blue', 0: 'red'}
// example 1: var $array2 = {a: 'GREEN', B: 'brown', 0: 'yellow', 1: 'red'}
// example 1: array_diff_uassoc($array1, $array2, function (key1, key2) { return (key1 === key2 ? 0 : (key1 > key2 ? 1 : -1)) })
// returns 1: {a: 'green', b: 'brown', c: 'blue', 0: 'red'}
const retArr = {};
const callback = arraysAndCallback.at(-1);
if (typeof callback === 'undefined' || !isPhpCallableDescriptor(callback)) {
throw new Error('array_diff_uassoc(): Invalid callback');
}
const arrays = arraysAndCallback.slice(0, -1).map((value) => toPhpArrayObject(value));
const keyComparator = resolveNumericComparator(callback, 'array_diff_uassoc(): Invalid callback');
arr1keys: for (const [k1, arr1Value] of entriesOfPhpAssoc(arr1)) {
for (const arr of arrays) {
for (const [k, arrValue] of entriesOfPhpAssoc(arr)) {
if (arrValue === arr1Value && 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;
}