locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
37 lines (36 loc) • 1.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.array_keys = array_keys;
const _phpTypes_ts_1 = require("../_helpers/_phpTypes.js");
function array_keys(input, searchValue, argStrict) {
// discuss at: https://locutus.io/php/array_keys/
// parity verified: PHP 8.3
// original by: Kevin van Zonneveld (https://kvz.io)
// input by: Brett Zamir (https://brett-zamir.me)
// input by: P
// bugfixed by: Kevin van Zonneveld (https://kvz.io)
// bugfixed by: Brett Zamir (https://brett-zamir.me)
// improved by: jd
// improved by: Brett Zamir (https://brett-zamir.me)
// example 1: array_keys( {firstname: 'Kevin', surname: 'van Zonneveld'} )
// returns 1: [ 'firstname', 'surname' ]
const search = typeof searchValue !== 'undefined';
const tmpArr = [];
const strict = !!argStrict;
let include = true;
for (const [key, value] of Object.entries(input)) {
include = true;
if (search) {
if (strict && value !== searchValue) {
include = false;
}
else if (value !== searchValue) {
include = false;
}
}
if (include) {
tmpArr.push((0, _phpTypes_ts_1.normalizeArrayKey)(key));
}
}
return tmpArr;
}