var ArrayFilterPipe = (function () {
function ArrayFilterPipe() {
}
ArrayFilterPipe._isOfType = function (value, type) {
return typeof (value) === type;
};
ArrayFilterPipe._isUndefined = function (value) {
return ArrayFilterPipe._isOfType(value, ArrayFilterPipe.TYPES.UNDEFINED);
};
ArrayFilterPipe._isObject = function (value) {
return ArrayFilterPipe._isOfType(value, ArrayFilterPipe.TYPES.OBJECT);
};
ArrayFilterPipe._isOrHasMatch = function (value, target) {
return ArrayFilterPipe._isOfType(value, ArrayFilterPipe.TYPES.STRING) || ArrayFilterPipe._isOfType(target, ArrayFilterPipe.TYPES.STRING)
? value.toString().toLowerCase().indexOf(target.toString().toLowerCase()) >= 0
: value == target;
};
ArrayFilterPipe._hasOptions = function (value, options) {
return (ArrayFilterPipe._isUndefined(value) || ArrayFilterPipe._isUndefined(options) ? (ArrayFilterPipe._isUndefined(value) && ArrayFilterPipe._isUndefined(options))
: (value === null || options == null) ? (value === null && options == null)
: (Array.isArray(value) || Array.isArray(options)) ? false
: ArrayFilterPipe._isObject(value) ?
(ArrayFilterPipe._isObject(options)
? Object.keys(options).every(function (key) { return value.hasOwnProperty(key) && ArrayFilterPipe._isOrHasMatch(value[key], options[key]); })
: Object.values(value).some(function (val) { return ArrayFilterPipe._isOrHasMatch(val, options); }))
: !ArrayFilterPipe._isObject(value) ?
(!ArrayFilterPipe._isObject(options)
? ArrayFilterPipe._isOrHasMatch(value, options)
: false)
: false);
};
ArrayFilterPipe._arrayToObject = function (value) {
return (ArrayFilterPipe._isUndefined(value) || value == null) ? value : value.reduce(function (result, current) {
result[current[0]] = current[1];
return result;
}, {});
};
ArrayFilterPipe.prototype.transform = function (value, options) {
if (!value || !Array.isArray(value) || ArrayFilterPipe._isUndefined(options) || options === null) {
return value;
}
options = Array.isArray(options) ? ArrayFilterPipe._arrayToObject(options) : options;
return value.filter(function (item) { return ArrayFilterPipe._hasOptions(item, options); });
};
return ArrayFilterPipe;
}());
ArrayFilterPipe.TYPES = {
OBJECT: 'object',
STRING: 'string',
UNDEFINED: 'undefined'
};
/*
TESTING
--------------------------------------------------
*/
var pipe = new ArrayFilterPipe();
var array = [null, undefined, , true, 123123, 'Jeke HeNry', 'joe', 'joe hen', {}, [], { fake: 'hen' }, { name: 'hen' }, { name: 'johenrik', country: 'hen' }, { name: 'joe dick', city: 'hen' }, { name: 'Jeke HeNry', country: 'zxy' }];
var options = null;
/* REF: http://stackoverflow.com/questions/11403107/capturing-javascript-console-log */
var oldLog = console.log;
console.log = function (message) {
var _arguments = arguments;
var div = Object.keys(arguments).map(function (key) { return Number(key); }).reduce(function (result, key) {
result = result || document.createElement('div');
var isJSON = (_arguments[key] != null && (typeof (_arguments[key]) === 'object' || Array.isArray(_arguments[key])));
var span = document.createElement(isJSON ? 'pre' : 'span');
span.innerHTML = isJSON ? JSON.stringify(_arguments[key], undefined) : _arguments[key].replace('\n', '</br></br>');
result.appendChild(span);
return result;
}, null);
document.body.appendChild(div);
oldLog.apply(console, arguments);
};
function test() {
console.log('options', options);
console.log('result', pipe.transform(array, options));
}
console.log('case : 01');
console.log('---------------------------------------------------');
options = 'hen';
test();
console.log('\ncase : 02');
console.log('---------------------------------------------------');
options = { name: 'hen' };
test();
options = [['name', 'hen']];
test();
console.log('\ncase : 03');
console.log('---------------------------------------------------');
options = { name: 'hen', country: 'hen' };
test();
options = [['name', 'hen'], ['country', 'hen']];
test();
console.log('\ncase : 04');
console.log('---------------------------------------------------');
options = { name: 'hen', city: 'hen', fake: true };
test();
options = [['name', 'hen'], ['country', 'hen'], ['fake', true]];
test();