UNPKG

1.02 kBJavaScriptView Raw
1'use strict';
2var uncurryThis = require('../internals/function-uncurry-this');
3var isArray = require('../internals/is-array');
4var isCallable = require('../internals/is-callable');
5var classof = require('../internals/classof-raw');
6var toString = require('../internals/to-string');
7
8var push = uncurryThis([].push);
9
10module.exports = function (replacer) {
11 if (isCallable(replacer)) return replacer;
12 if (!isArray(replacer)) return;
13 var rawLength = replacer.length;
14 var keys = [];
15 for (var i = 0; i < rawLength; i++) {
16 var element = replacer[i];
17 if (typeof element == 'string') push(keys, element);
18 else if (typeof element == 'number' || classof(element) === 'Number' || classof(element) === 'String') push(keys, toString(element));
19 }
20 var keysLength = keys.length;
21 var root = true;
22 return function (key, value) {
23 if (root) {
24 root = false;
25 return value;
26 }
27 if (isArray(this)) return value;
28 for (var j = 0; j < keysLength; j++) if (keys[j] === key) return value;
29 };
30};