31 lines
1.0 KiB
JavaScript
31 lines
1.0 KiB
JavaScript
|
'use strict';
|
||
|
var uncurryThis = require('../internals/function-uncurry-this');
|
||
|
var isArray = require('../internals/is-array');
|
||
|
var isCallable = require('../internals/is-callable');
|
||
|
var classof = require('../internals/classof-raw');
|
||
|
var toString = require('../internals/to-string');
|
||
|
|
||
|
var push = uncurryThis([].push);
|
||
|
|
||
|
module.exports = function (replacer) {
|
||
|
if (isCallable(replacer)) return replacer;
|
||
|
if (!isArray(replacer)) return;
|
||
|
var rawLength = replacer.length;
|
||
|
var keys = [];
|
||
|
for (var i = 0; i < rawLength; i++) {
|
||
|
var element = replacer[i];
|
||
|
if (typeof element == 'string') push(keys, element);
|
||
|
else if (typeof element == 'number' || classof(element) === 'Number' || classof(element) === 'String') push(keys, toString(element));
|
||
|
}
|
||
|
var keysLength = keys.length;
|
||
|
var root = true;
|
||
|
return function (key, value) {
|
||
|
if (root) {
|
||
|
root = false;
|
||
|
return value;
|
||
|
}
|
||
|
if (isArray(this)) return value;
|
||
|
for (var j = 0; j < keysLength; j++) if (keys[j] === key) return value;
|
||
|
};
|
||
|
};
|