array-includes-polyfill
Version:
Exposing a richer set of Array features for JavaScript
110 lines (86 loc) • 2.11 kB
JavaScript
class Lookup{
lookup(obj, withIndex) {
let O = Object(this);
var result = [];
O.map(function(object, index){
if(_isFlat(object)){
if(_compareObjs(obj, object)){
if(withIndex)
_define$$index(object, index);
result.push(object);
}
}
if(!_isFlat(object)){
var tempObj1 = _flattenObjs(object, withIndex, index);
var tempObj2 = _flattenObjs(obj, withIndex, index);
if(_compareObjs(tempObj2, tempObj1)){
_define$$index(object, index);
result.push(object);
}
}
});
var x = result;
result = [];
if(x.length === 1)
x = x[0];
if(x === undefined || x === null || x == '')
x = false;
return x;
}
}
function _define$$index(object, index){
Object.defineProperty(object, "_$$index_", {
enumerable: true,
configurable: true,
writable: false,
value:index,
});
}
function _compareObjs (obj1, obj2){
var validate = [];
var keys = Object.keys(obj1);
var state;
keys.map(function(key){
if(obj1[key] === obj2[key])
validate.push(true);
});
(validate.length === keys.length) ? state=true : state=false;
return state;
}
let _flattend = {};
let _recursion = false;
function _flattenObjs(object, withIndex, index){
if(!_recursion) _flattend = {};
var keys = Object.keys(object);
keys.map(function(key){
if(!_isObject(object[key]))
_flattend[key] = object[key];
if(_isObject(object[key])){
_recursion = true;
_flattenObjs(object[key]);
}
});
_recursion = false;
if(withIndex)
_define$$index(_flattend, index);
return _flattend;
}
function _isFlat(object){
var validate = [];
if(object instanceof Object){
var keys = Object.keys(object);
keys.map(function(key){
if(object[key] instanceof Object)
validate.push(true);
});
}
if (validate.length > 0)
return false;
return true;
}
function _isObject(object){
if(object instanceof Object)
return true;
return false;
}
module.exports = Lookup;