Comparing version 1.0.0 to 1.1.0
84
index.js
@@ -1,54 +0,56 @@ | ||
var ifonly = function (obj, keys, ignore) { | ||
var state, key; | ||
var isEmpty = function (some) { | ||
if (some == null || typeof some === 'undefined') { | ||
return true; | ||
} | ||
;(function () { | ||
var only = function (obj, keys, ignore) { | ||
var state, key; | ||
var isEmpty = function (some) { | ||
if (some == null || typeof some === 'undefined') { | ||
return true; | ||
} | ||
if (some instanceof Array || typeof some === 'string') { | ||
return some.length === 0; | ||
} | ||
if (some instanceof Array || typeof some === 'string') { | ||
return some.length === 0; | ||
} | ||
for (var k in some) { | ||
if (some.hasOwnProperty(k)) { | ||
return false; | ||
} | ||
} | ||
for (var k in some) { | ||
if (some.hasOwnProperty(k)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
ignore = ignore || []; | ||
return true; | ||
}; | ||
ignore = ignore || []; | ||
for (key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
// if current key is in ignore list | ||
if (ignore.indexOf(key) !== -1) { | ||
continue; | ||
} | ||
for (key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
// if current key is in ignore list | ||
if (ignore.indexOf(key) !== -1) { | ||
continue; | ||
} | ||
// if there is other key in obj which is not empty | ||
if (keys.indexOf(key) === -1 && !isEmpty(obj[key])) { | ||
state = false; | ||
break; | ||
} | ||
// if there is other key in obj which is not empty | ||
if (keys.indexOf(key) === -1 && !isEmpty(obj[key])) { | ||
state = false; | ||
break; | ||
} | ||
// if current key is required and not empty | ||
if (keys.indexOf(key) !== -1 && !isEmpty(obj[key])) { | ||
state = true; | ||
} | ||
// if current key is required and not empty | ||
if (keys.indexOf(key) !== -1 && !isEmpty(obj[key])) { | ||
state = true; | ||
} | ||
// if current key is required but it's empty | ||
if (keys.indexOf(key) !== -1 && isEmpty(obj[key])) { | ||
state = false; | ||
// if current key is required but it's empty | ||
if (keys.indexOf(key) !== -1 && isEmpty(obj[key])) { | ||
state = false; | ||
} | ||
} | ||
} | ||
} | ||
return state; | ||
}; | ||
return state; | ||
}; | ||
if (typeof module !== 'undefined' && module.exports) { | ||
module.exports = ifonly; | ||
if (typeof module !== 'undefined' && module.exports) { | ||
module.exports = only; | ||
} else { | ||
window.ifonly = ifonly; | ||
window.only = only; | ||
} | ||
})(); |
{ | ||
"name": "ifonly", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "ifonly is function that checks if your object have only and only specific properties you passed.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -15,15 +15,15 @@ # ifonly | ||
obj = { a: 'a'}; | ||
console.log(ifonly(obj, ['a'])); // true because only 'a' property is present | ||
console.log(only(obj, ['a'])); // true because only 'a' property is present | ||
obj = { a: 'a', b: 'b', c: 'c' }; | ||
console.log(ifonly(obj, ['a', 'b'])); // false because 'c' property is present | ||
console.log(only(obj, ['a', 'b'])); // false because 'c' property is present | ||
obj = { a: 'a', b: 'b', c: 'c' }; | ||
console.log(ifonly(obj, ['a', 'b'], ['c'])); // true because 'c' property is ignored | ||
console.log(only(obj, ['a', 'b'], ['c'])); // true because 'c' property is ignored | ||
obj = { a: '', b: 'b', c: '' }; | ||
console.log(ifonly(obj, ['b'])); // true because 'a' and 'c' properties are empty | ||
console.log(only(obj, ['b'])); // true because 'a' and 'c' properties are empty | ||
obj = { a: [], b: 'b', c: {} }; | ||
console.log(ifonly(obj, ['b'])); // true because 'a' and 'c' properties are empty | ||
console.log(only(obj, ['b'])); // true because 'a' and 'c' properties are empty | ||
``` |
4094
72