fluro
Version:
Promise based HTTP Fluro client for the browser and node.js
2,122 lines (1,577 loc) • 86.9 kB
JavaScript
import _ from 'lodash';
import moment from 'moment-timezone';
import stringSimilarity from 'string-similarity';
var chrono; //= require('chrono-node');
var verboseDEBUG;
//////////////////////////////////////////////////////////////////////
var service = {};
//////////////////////////////////////////////////////////////////////
service.activeFilters = function(config) {
var memo = [];
getActiveFilter(config, memo);
return memo;
////////////////////////////
function getActiveFilter(block, memo) {
var isValid = service.isValidFilter(block);
if (isValid) {
memo.push(block);
}
if (block.filters && block.filters.length) {
_.each(block.filters, function(b) {
getActiveFilter(b, memo);
})
}
}
}
//////////////////////////////////////////////////////////////////////
function isNotANumber(input) {
return isNaN(parseInt(input));
}
//////////////////////////////////////////////////////////////////////
service.activeFilterRows = function(config) {
return _.filter(service.activeFilters, function(row) {
return row.comparator && row.comparator.length;
})
}
service.activeFilterKeys = function(config) {
var keys = _.chain(service.activeFilters(config))
.map(function(entry) {
if (!entry || !entry.key) {
return;
}
var rootKey = service.getRootKey(entry.key);
// //console.log('ROOT KEY', rootKey);
return rootKey;
})
.compact()
.uniq()
.value();
return keys;
}
//////////////////////////////////////////////////////////////////////
service.activeFilterCriteriaString = function(config) {
var criteriaValue = _.chain(service.activeFilters(config))
.map(function(block) {
if (!block.criteria || !block.criteria.length) {
return;
}
//////////////////////////
var activeCriteria = service.activeFilters({ filters: block.criteria });
// ////console.log('ACTIVE CRITERIA', block.criteria, activeCriteria)
if (!activeCriteria || !activeCriteria.length) {
return;
}
//////////////////////////
return service.getFilterChangeString({ filters: activeCriteria });
})
.flatten()
.compact()
.map(function(value) {
var nameTitle = value.title || value.name || value._id || value;
return String(nameTitle).toLowerCase();
})
.compact()
.uniq()
.value();
////////////////////////
// if (criteriaValue && criteriaValue.length) {
// ////console.log('FILTER VALUES', criteriaValue);
// }
////////////////////////
return criteriaValue;
}
//////////////////////////////////////////////////////////////////////
service.activeFilterValues = function(config) {
var values = _.chain(service.activeFilters(config))
.map(function(block) {
var all = [];
var comparator = service.getComparator(block.comparator);
if (!comparator) {
return;
}
switch (comparator.inputType) {
case 'array':
all = all.concat(block.values);
break;
case 'range':
case 'daterange':
all = all.concat([block.value, block.value2]);
break;
default:
var blockValue1 = block.value;
var blockValue2 = block.value2;
if (block.dataType == 'boolean') {
blockValue1 = String(service.convertToBoolean(blockValue1));
blockValue2 = String(service.convertToBoolean(blockValue2));
}
all = all.concat([blockValue1, blockValue2]);
break;
}
return all;
})
.flatten()
.compact()
.map(function(value) {
var nameTitle = value.title || value.name || value._id || value;
return String(nameTitle).toLowerCase();
})
.compact()
.uniq()
.value();
// ////console.log('FILTER VALUES', values);
return values;
}
service.activeFilterComparators = function(config) {
var memo = [];
getActiveFilterComparator(config, memo);
return memo;
////////////////////////////
function getActiveFilterComparator(block, memo) {
var isValid = service.isValidFilter(block);
if (isValid) {
memo.push(block.comparator);
}
if (block.filters && block.filters.length) {
_.each(block.filters, function(b) {
getActiveFilterComparator(b, memo);
})
}
}
}
service.activeFilterOperators = function(config) {
var memo = [];
var trail = [];
//////////////////////////
getActiveFilterBlockOperator(config, memo, trail);
//////////////////////////
var flat = _.chain(memo)
.flatten()
.reduce(function(set, operator) {
if (!set[operator]) {
set[operator] = 0;
}
set[operator]++;
return set;
}, {})
.map(function(i, key) {
return `${i}${key}`;
})
.compact()
.value();
////////////////////////////
return flat;
////////////////////////////
function getActiveFilterBlockOperator(block, memo, trail) {
var operator = block.operator;
//If it's a rule set
if (operator) {
// //Add the path to the block
trail.push(operator);
///////////////////////////////
//Check if any of it's filters are valid and active
var isValid = _.some(block.filters, function(filter) {
return service.isValidFilter(filter);
})
if (isValid) {
memo.push(trail.slice());
}
} else {
trail.length = 0;
}
////////////////////////////////////////////////
//Go down the tree further
if (block.filters && block.filters.length) {
_.each(block.filters, function(b) {
getActiveFilterBlockOperator(b, memo, trail);
})
} else {
trail.length = 0;
}
}
}
//////////////////////////////////////////////////////////////////////
service.getFilterChangeString = function(config) {
//Put all this together so we only refilter when we actually need to
//each of these will only return if the filter is valid and actually changes
//effects the results, without this step the table will update everytime you change the filters
var string = [
service.activeFilterKeys(config).join(', '),
service.activeFilterValues(config).join(', '),
service.activeFilterComparators(config).join(', '),
service.activeFilterOperators(config).join(', '),
service.activeFilterCriteriaString(config).join(', '),
].join('');
return string;
}
//////////////////////////////////////////////////////////////////////
//Helper function to map an input to a basic string
function getString(input, includeIDs) {
if (!input) {
return '';
}
if (includeIDs) {
if (input._id) {
return String(input._id).toLowerCase();
}
if (input._external) {
return String(input._external).toLowerCase();
}
if (input.id) {
return String(input.id).toLowerCase();
}
}
if (input.title) {
return String(input.title).toLowerCase();
}
if (input.name) {
return String(input.name).toLowerCase();
}
return String(input).toLowerCase()
}
//////////////////////////////////////////////////////////////////////
function getAllStringMatches(input, includeIDs) {
var matches = [];
if (!input) {
return matches;
}
//If it's text or a number
if (!_.isObject(input)) {
return [getString(input, includeIDs)];
}
//If it's an array
if (_.isArray(input)) {
return _.flatten(getAllStringMatches(input, includeIDs))
}
//Otherwise it's likely an object
matches.push(getString(input, includeIDs));
if (matches.length == 1) {
return matches[0];
}
// if (includeIDs) {
// if (input._id && input._id.length) {
// matches.push(String(input._id).toLowerCase());
// }
// if (input._external && input._external.length) {
// matches.push(String(input._external).toLowerCase());
// }
// }
// if (input.title && input.title.length) {
// matches.push(String(input.title).toLowerCase());
// }
// if (input.name && input.name.length) {
// matches.push(String(input.name).toLowerCase());
// }
return matches;
}
//////////////////////////////////////////////////////////////////////
function isBetween(input, from, to) {
var startFloat = parseFloat(from);
var endFloat = parseFloat(to);
var checkFloat = parseFloat(input || 0);
var start = Math.min(startFloat, endFloat);
var end = Math.max(startFloat, endFloat);
var match = (checkFloat >= start) && (checkFloat <= end);
return match;
}
//////////////////////////////////////////////////////////////////////
function isIn(input, range) {
var stringInput = getString(input, true);
//Range is the array we are checking in
return _.some(range, function(entry) {
var entryString = getString(entry, true);
// ////console.log('CHECK', range, entry, stringInput, entryString)
return stringInput == getString(entry, true);
});
}
///////////////////////////////////
function isEmpty(input) {
if (!input) {
return true;
}
if (input == undefined) {
return true;
}
if (input == null) {
return true;
}
if (input == '') {
return true;
}
if (_.isArray(input) && !input.length) {
return true;
}
if (input == 0 || input == '0') {
return true;
}
}
///////////////////////////////////
service.matchAnyString = function(keywords, row) {
//Get the keywords we want to search on
var searchString = getString(keywords);
if (_.isString(row)) {
return checkString(row);
} else {
var values = _.values(row);
return _.some(values, checkString)
}
/////////////////////////////////////
function checkString(input) {
var stringInput = getString(input);
var exactMatch = _.includes(stringInput, searchString);
// console.log('EXACT MATCH?', stringInput, string)
return exactMatch || service.isSimilar(stringInput, searchString)
}
}
///////////////////////////////////
service.isSimilar = function(input, mustMatchValue, options) {
if (!options) {
options = {};
}
var score = stringSimilarity.compareTwoStrings(getString(input), getString(mustMatchValue));
var matches = (score >= 0.6);
if (options.source) {
if (!options._similar) {
options._similar = 0;
}
//Increase the score
options._similar += matches;
}
return matches;
}
///////////////////////////////////
///////////////////////////////////
function isEqual(input, range) {
//Input is the field from the row, range is the filter input typed by the user
// ////console.log('INPUT', input, range);
var matchAny = getAllStringMatches(input, true);
var rangeAsString = getString(range, true);
// //console.log('CHECK MATCH', matchAny, rangeAsString);
if (matchAny == rangeAsString) {
return true;
}
return _.includes(matchAny, rangeAsString);
}
function isContained(input, range) {
//TODO Check if this is necessary
//As i think this will always be a string
if (_.isString(range)) {
range = getString(range);
}
return _.includes(range, getString(input));
}
///////////////////////////////
function dateCompare(input, range, type, format, timezone) {
if (!input) {
return false;
}
// var date1 = new Date(input);
// date1.setHours(0, 0, 0, 0);
// var date2 = new Date(range);
// date2.setHours(0, 0, 0, 0);
// ////console.log('CHECK DATE>>>>', input, range)
var moment1 = moment.tz(input, timezone); //Birthday
var moment2 = moment.tz(range, timezone); //Relative Date
switch (type) {
case 'next':
case 'past':
//We can go down to hourly
break;
default:
//Just track the day
moment1.startOf('day');
moment2.startOf('day');
break;
}
///////////////////////////////////////////////////
var date1 = moment1.toDate();
var date2 = moment2.toDate();
var now = new Date();
///////////////////////////////////////////////////
var matched;
switch (type) {
case 'date':
matched = String(date1) == String(date2);
if (verboseDEBUG) {
////console.log('Matched', type, matched, String(date1), String(date2))
}
break;
case 'week':
matched = moment1.format('W YYYY') == moment2.format('W YYYY');
if (verboseDEBUG) {
////console.log('Matched', type, matched, moment1.format('W YYYY'), moment2.format('W YYYY'))
}
break;
case 'month':
matched = moment1.format('M YYYY') == moment2.format('M YYYY');
if (verboseDEBUG) {
////console.log('Matched', type, matched, moment1.format('M YYYY'), moment2.format('M YYYY'))
}
break;
case 'year':
matched = moment1.format('YYYY') == moment2.format('YYYY');
if (verboseDEBUG) {
////console.log('Matched', type, matched, moment1.format('YYYY'), moment2.format('YYYY'))
}
break;
case 'dateanniversary':
matched = moment1.format('D MMM') == moment2.format('D MMM');
if (verboseDEBUG) {
////console.log('Matched', type, matched, moment1.format('D MMM'), moment2.format('D MMM'))
}
break;
case 'dateanniversarynext':
var startRange = moment();
var dateDiffYears = startRange.diff(moment1, 'years', true);
var checkDate = moment1.add(Math.ceil(dateDiffYears), 'years').toDate();
// //console.log('CHECK DATE Anniversary NEXT', date1, 'CHECK DATES', dateDiffYears, 'start:', startRange.toDate(), 'Anniversary:', checkDate, 'end:', date2);
//If the date is earlier than now
if (checkDate < now) {
matched = false;
} else {
matched = checkDate.getTime() <= date2.getTime()
}
if (verboseDEBUG) {
////console.log('Matched', type, matched, date1, date2)
}
break;
case 'dateanniversarypast':
//moment1 the birthday
//moment 2 is 5 days ago
///////////////////////////////////
var startRange = moment2;
var dateDiffYears = startRange.diff(moment1, 'years', true);
var checkDate = moment1.add(Math.ceil(dateDiffYears), 'years').toDate();
//If the date is earlier than now
if (checkDate > now) {
matched = false;
} else {
matched = checkDate.getTime() >= date2.getTime()
}
// if (matched) {
// //console.log('CHECK DATE Anniversary PAST', startRange.toDate(), '-', checkDate, '-', now);
// }
if (verboseDEBUG) {
////console.log('Matched', type, matched, date1, date2)
}
break;
case 'dateanniversarymonth':
matched = moment1.format('MMM') == moment2.format('MMM');
if (verboseDEBUG) {
////console.log('Matched', type, matched, moment1.format('MMM'), moment2.format('MMM'))
}
break;
// case 'weekday':
// matched = moment(date1).format('dddd') == moment(date2).format('dddd');
// break;
case 'before':
matched = date1.getTime() < date2.getTime()
if (verboseDEBUG) {
////console.log('Matched', type, matched, date1, date2)
}
break;
case 'after':
matched = date1.getTime() >= date2.getTime()
if (verboseDEBUG) {
////console.log('Matched', type, matched, date1, date2)
}
break;
case 'next':
//If the date is earlier than now
if (date1 < now) {
matched = false;
} else {
matched = date1.getTime() < date2.getTime()
}
if (verboseDEBUG) {
////console.log('Matched', type, matched, date1, date2)
}
break;
case 'past':
//If the date is later than now
if (date1 > now) {
matched = false;
} else {
matched = date1.getTime() >= date2.getTime()
}
if (verboseDEBUG) {
////console.log('Matched', type, matched, date1, date2)
}
break;
}
///////////////////////////////////////////////////
return matched;
}
///////////////////////////////
service.comparators = [];
///////////////////////////////
///////////////////////////////
///////////////////////////////
///////////////////////////////
///////////////////////////////
///////////////////////////////
///////////////////////////////
///////////////////////////////
///////////////////////////////
//Date Comparators
service.comparators.push({
title: 'Is on day ',
operator: 'datesameday',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
if (_.isArray(input)) {
return _.some(input, function(i) {
// dateCompare(input, range, type, format, timezone)
return dateCompare(i, mustMatchValue, 'date', null, options.timezone);
});
} else {
// dateCompare(input, range, type, format, timezone)
return dateCompare(input, mustMatchValue, 'date', null, options.timezone);
}
},
// dateDisplayFormat:'D MMM YYYY',
restrict: [
'date',
],
})
service.comparators.push({
title: 'Anniversary Date',
operator: 'dateanniversary',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
if (_.isArray(input)) {
return _.some(input, function(i) {
// dateCompare(input, range, type, format, timezone)
return dateCompare(i, mustMatchValue, 'dateanniversary', null, options.timezone);
});
} else {
return dateCompare(input, mustMatchValue, 'dateanniversary', null, options.timezone);
}
},
dateDisplayFormat: 'YYYY',
restrict: [
'date',
],
})
// service.comparators.push({
// title: 'Is between',
// operator: 'datebetween',
// match(input, mustMatchValue1, mustMatchValue2) {
// var checkDate = new Date(input);
// checkDate.setHours(0, 0, 0, 0);
// var date1 = new Date(mustMatchValue1)
// date1.setHours(0, 0, 0, 0);
// var date2 = new Date(mustMatchValue2)
// date2.setHours(0, 0, 0, 0);
// return isBetween(checkDate.getTime(), date1.getTime(), date2.getTime());
// },
// restrict: [
// 'date',
// ],
// inputType: 'daterange',
// })
service.comparators.push({
title: 'Anniversary is Between',
operator: 'dateanniversarybetween',
match(input, mustMatchValue1, mustMatchValue2, NOT_USED, options) {
if (!options) {
options = {}
}
if (!input) {
return;
}
var checkDate = new Date(input);
if (isNaN(checkDate)) {
return;
}
checkDate.setHours(0, 0, 0, 0);
var date1 = new Date(mustMatchValue1)
date1.setHours(0, 0, 0, 0);
var date2 = new Date(mustMatchValue2)
date2.setHours(0, 0, 0, 0);
////////////////////////////////////////
var startDate = new Date(Math.min(date1, date2));
var endDate = new Date(Math.max(date1, date2));
////////////////////////////////////////
function zeroPadded(str) {
str = String(str);
if (str.length == 1) {
return '0' + str;
}
return str;
}
////////////////////////////////////////
var checkTimestamp = parseInt(`${zeroPadded(checkDate.getMonth())}${zeroPadded(checkDate.getDate())}`);
var startTimestamp = parseInt(`${zeroPadded(startDate.getMonth())}${zeroPadded(startDate.getDate())}`);
var endTimestamp = parseInt(`${zeroPadded(endDate.getMonth())}${zeroPadded(endDate.getDate())}`);
return checkTimestamp >= startTimestamp && checkTimestamp <= endTimestamp;
},
dateDisplayFormat: 'YYYY',
restrict: [
'date',
],
inputType: 'daterange',
})
// service.comparators.push({
// title: 'Anniversary Month',
// operator: 'dateanniversarymonth',
// match(input, mustMatchValue) {
// // ////console.log('TEST MONTH', mustMatchValue);
// if (_.isArray(input)) {
// //Check if any of the dates on the row
// return _.some(input, function(i) {
// //match any of the dates provided in the array
// return _.some(mustMatchValue, function(d) {
// return dateCompare(i, d, 'dateanniversarymonth');
// });
// });
// } else {
// //check if the date on the row matches any of the dates
// //in our array
// return _.some(mustMatchValue, function(d) {
// return dateCompare(input, d, 'dateanniversarymonth');
// });
// }
// },
// dateDisplayFormat: 'MMMM',
// restrict: [
// 'date',
// ],
// inputType: 'array',
// })
/**
service.comparators.push({
title: 'Is same week as',
operator: 'datesameweek',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
if (_.isArray(input)) {
return _.some(input, function(i) {
// dateCompare(input, range, type, format, timezone)
return dateCompare(i, mustMatchValue, 'week', null, options.timezone);
});
} else {
return dateCompare(input, mustMatchValue, 'week', null, options.timezone);
}
},
dateDisplayFormat: '[Wk]W YYYY',
restrict: [
'date',
],
})
service.comparators.push({
title: 'Is same month as',
operator: 'datesamemonth',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
// match(input, mustMatchValue) {
// var mustMatchString = String(mustMatchValue);
// // ////console.log('Check date input', input);
// if (_.isArray(input)) {
// return _.some(input, function(i) {
// var weekdayInteger = moment(i).weekday()
// return _.includes(mustMatchString, weekdayInteger);
// });
// } else {
// var weekdayInteger = moment(input).weekday()
// return _.includes(mustMatchString, weekdayInteger);
// }
// },
if (_.isArray(input)) {
return _.some(input, function(i) {
// dateCompare(input, range, type, format, timezone)
return dateCompare(i, mustMatchValue, 'month', null, options.timezone);
});
} else {
return dateCompare(input, mustMatchValue, 'month', null, options.timezone);
}
},
dateDisplayFormat: 'MMM YYYY',
restrict: [
'date',
],
})
service.comparators.push({
title: 'Is same year as',
operator: 'datesameyear',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
if (_.isArray(input)) {
return _.some(input, function(i) {
// dateCompare(input, range, type, format, timezone)
return dateCompare(i, mustMatchValue, 'year', null, options.timezone);
});
} else {
return dateCompare(input, mustMatchValue, 'year', null, options.timezone);
}
},
dateDisplayFormat: 'YYYY',
restrict: [
'date',
],
})
/**/
/**
service.comparators.push({
title: 'Is weekday',
operator: 'datesameweekday',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
var mustMatchString = String(mustMatchValue);
// ////console.log('Check date input', input);
if (_.isArray(input)) {
return _.some(input, function(i) {
var weekdayInteger = String(moment(i).weekday());
return _.includes(mustMatchString, weekdayInteger);
});
} else {
var weekdayInteger = String(moment(input).weekday());
return _.includes(mustMatchString, weekdayInteger);
}
},
dateDisplayFormat: 'dddd',
restrict: [
'date',
],
})
service.comparators.push({
title: 'Is not weekday',
operator: 'datedifferentweekday',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
var mustMatchString = String(mustMatchValue);
if (_.isArray(input)) {
return !_.some(input, function(i) {
var weekdayInteger = String(moment(i).weekday())
return _.includes(mustMatchString, weekdayInteger);
});
} else {
var weekdayInteger = String(moment(input).weekday())
return !_.includes(mustMatchString, weekdayInteger);
}
},
dateDisplayFormat: 'dddd',
restrict: [
'date',
],
})
/**/
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
service.comparators.push({
title: 'Is before',
operator: 'datebefore',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
if (!input) {
return;
}
if (_.isArray(input)) {
if (!input.length) {
return;
}
return _.some(input, function(i) {
// dateCompare(input, range, type, format, timezone)
return dateCompare(i, mustMatchValue, 'before', null, options.timezone);
});
} else {
return dateCompare(input, mustMatchValue, 'before', null, options.timezone);
}
},
restrict: [
'date',
],
})
service.comparators.push({
title: 'Is after',
operator: 'dateafter',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
if (!input) {
return;
}
if (_.isArray(input)) {
if (!input.length) {
return;
}
return _.some(input, function(i) {
// dateCompare(input, range, type, format, timezone)
return dateCompare(i, mustMatchValue, 'after', null, options.timezone);
});
} else {
// ////console.log('COMPARE DATE', input)
var RESULT = dateCompare(input, mustMatchValue, 'after', null, options.timezone);
// ////console.log('CHECK', RESULT, input, mustMatchValue, options);
return RESULT;
}
},
restrict: [
'date',
],
})
service.comparators.push({
title: 'Is not before',
operator: 'datenotbefore',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
if (!input) {
return true;
}
if (_.isArray(input)) {
if (!input.length) {
return true;
}
//Every entry is not before
return _.every(input, function(i) {
// dateCompare(input, range, type, format, timezone)
return !dateCompare(i, mustMatchValue, 'before', null, options.timezone);
});
} else {
return !dateCompare(input, mustMatchValue, 'before', null, options.timezone);
}
},
restrict: [
'date',
],
})
service.comparators.push({
title: 'Is not after',
operator: 'datenotafter',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
if (!input) {
// ////console.log('TEST > NO INPUT')
return true;
}
if (_.isArray(input)) {
if (!input.length) {
// ////console.log('TEST > NO INPUT LENGTH')
return true;
}
//Every entry is not after
var allMatch = _.every(input, function(i) {
// dateCompare(input, range, type, format, timezone)
return !dateCompare(i, mustMatchValue, 'after', null, options.timezone);
});
// ////console.log('TEST > ALL MATCH?', allMatch)
return allMatch;
} else {
var matchSingle = !dateCompare(input, mustMatchValue, 'after', null, options.timezone);
// ////console.log('TEST > MATCH SINGLE', matchSingle, input, mustMatchValue)
return matchSingle;
}
},
restrict: [
'date',
],
})
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
service.comparators.push({
title: 'Anniversary is in the next',
inputType: 'datemeasure',
operator: 'dateanniversarynext',
match(input, measure, period, options) {
if (!options) {
options = {}
}
if (!input) {
return;
}
var mustMatchValue = moment().add(measure, period).toDate();
if (_.isArray(input)) {
if (!input.length) {
return;
}
return _.some(input, function(i) {
// dateCompare(input, range, type, format, timezone)
return dateCompare(i, mustMatchValue, 'dateanniversarynext', null, options.timezone);
});
} else {
return dateCompare(input, mustMatchValue, 'dateanniversarynext', null, options.timezone);
}
},
restrict: [
'date',
],
})
service.comparators.push({
title: 'Anniversary is in the last',
inputType: 'datemeasure',
operator: 'dateanniversarypast',
match(input, measure, period, options) {
if (!options) {
options = {}
}
if (!input) {
return;
}
var mustMatchValue = moment().subtract(measure, period).toDate();
if (_.isArray(input)) {
if (!input.length) {
return;
}
return _.some(input, function(i) {
// dateCompare(input, range, type, format, timezone)
return dateCompare(i, mustMatchValue, 'dateanniversarypast', null, options.timezone);
});
} else {
return dateCompare(input, mustMatchValue, 'dateanniversarypast', null, options.timezone);
}
},
restrict: [
'date',
],
})
service.comparators.push({
title: 'Is in the next',
inputType: 'datemeasure',
operator: 'datenext',
match(input, measure, period, options) {
if (!options) {
options = {}
}
//////////////////////////
var value;
//////////////////////////
if (!input) {
return value;
}
var mustMatchValue = moment().add(measure, period).toDate();
if (_.isArray(input)) {
if (!input.length) {
return value;
}
value = _.some(input, function(i) {
// dateCompare(input, range, type, format, timezone)
return dateCompare(i, mustMatchValue, 'next', null, options.timezone);
});
} else {
value = dateCompare(input, mustMatchValue, 'next', null, options.timezone);
}
return value;
},
restrict: [
'date',
],
})
service.comparators.push({
title: 'Is in the last',
inputType: 'datemeasure',
operator: 'datepast',
match(input, measure, period, options) {
if (!options) {
options = {}
}
//////////////////////////
var value;
//////////////////////////
if (!input) {
return value;
}
var mustMatchValue = moment().subtract(measure, period).toDate();
if (_.isArray(input)) {
if (!input.length) {
return value;
}
value = _.some(input, function(i) {
// dateCompare(input, range, type, format, timezone)
return dateCompare(i, mustMatchValue, 'past', null, options.timezone);
});
} else {
value = dateCompare(input, mustMatchValue, 'past', null, options.timezone);
}
return value;
},
restrict: [
'date',
],
})
////////////////////////////////////////////////////////////
service.comparators.push({
title: 'Is not in the next',
inputType: 'datemeasure',
operator: 'datenotnext',
match(input, measure, period, options) {
if (!options) {
options = {}
}
//////////////////////////
var value;
//////////////////////////
if (!input) {
return !value;
}
var mustMatchValue = moment().add(measure, period).toDate();
if (_.isArray(input)) {
if (!input.length) {
return !value;
}
value = _.some(input, function(i) {
// dateCompare(input, range, type, format, timezone)
return dateCompare(i, mustMatchValue, 'next', null, options.timezone);
});
} else {
value = dateCompare(input, mustMatchValue, 'next', null, options.timezone);
}
return !value;
},
restrict: [
'date',
],
})
service.comparators.push({
title: 'Is not in the last',
inputType: 'datemeasure',
operator: 'datenotpast',
match(input, measure, period, options) {
if (!options) {
options = {}
}
//////////////////////////
var value;
//////////////////////////
if (!input) {
return !value;
}
var mustMatchValue = moment().subtract(measure, period).toDate();
if (_.isArray(input)) {
if (!input.length) {
return !value;
}
value = _.some(input, function(i) {
// dateCompare(input, range, type, format, timezone)
return dateCompare(i, mustMatchValue, 'past', null, options.timezone);
});
} else {
value = dateCompare(input, mustMatchValue, 'past', null, options.timezone);
}
return !value;
},
restrict: [
'date',
],
})
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
service.comparators.push({
title: 'Is between',
operator: 'datebetween',
match(input, mustMatchValue1, mustMatchValue2, options) {
if (!options) {
options = {}
}
// ////console.log('----------------------------------------')
// ////console.log('TIMEZONE Check timezone options', options.timezone)
// ////console.log('Before', input, mustMatchValue1, mustMatchValue2);
var date1 = new Date(mustMatchValue1)
date1.setHours(0, 0, 0, 0);
var date2 = new Date(mustMatchValue2)
date2.setHours(0, 0, 0, 0);
// ////console.log('After', input, date1, date2);
if (_.isArray(input)) {
return _.some(input, function(i) {
var checkDate = new Date(i);
checkDate.setHours(0, 0, 0, 0);
return isBetween(checkDate.getTime(), date1.getTime(), date2.getTime());
});
} else {
var checkDate = new Date(input);
checkDate.setHours(0, 0, 0, 0);
return isBetween(checkDate.getTime(), date1.getTime(), date2.getTime());
}
},
restrict: [
'date',
],
inputType: 'daterange',
})
service.comparators.push({
title: 'Is not between',
operator: 'datenotbetween',
match(input, mustMatchValue1, mustMatchValue2, options) {
if (!options) {
options = {}
}
var date1 = new Date(mustMatchValue1)
date1.setHours(0, 0, 0, 0);
var date2 = new Date(mustMatchValue2)
date2.setHours(0, 0, 0, 0);
if (_.isArray(input)) {
return !_.some(input, function(i) {
var checkDate = new Date(i);
checkDate.setHours(0, 0, 0, 0);
return isBetween(checkDate.getTime(), date1.getTime(), date2.getTime());
});
} else {
var checkDate = new Date(input);
checkDate.setHours(0, 0, 0, 0);
return !isBetween(checkDate.getTime(), date1.getTime(), date2.getTime());
}
},
restrict: [
'date',
],
inputType: 'daterange',
})
///////////////////////////////
///////////////////////////////
///////////////////////////////
service.comparators.push({
title: 'Is one of',
operator: 'in',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
if (_.isArray(input)) {
//Check if any match
return _.some(input, function(i) {
return isIn(i, mustMatchValue);
});
} else {
var matches = isIn(input, mustMatchValue);
// ////console.log('Must match value', matches, input, mustMatchValue);
return matches;
}
},
restrict: [
'string',
'email',
'url',
// 'number',
// 'integer',
// 'decimal',
// 'float',
'reference',
],
inputType: 'array',
})
service.comparators.push({
title: 'Is not one of',
operator: 'notin',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
if (_.isArray(input)) {
return !_.some(input, function(i) {
return isIn(i, mustMatchValue);
});
} else {
return !isIn(input, mustMatchValue);
}
},
restrict: [
'string',
'email',
'url',
//'number',
//'integer',
//'decimal',
//'float',
'reference',
],
inputType: 'array',
})
service.comparators.push({
title: 'Is ',
operator: '==',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
if (_.isArray(input)) {
return _.some(input, function(i) {
return isEqual(i, mustMatchValue);
});
} else {
return isEqual(input, mustMatchValue);
}
},
restrict: [
'string',
'email',
'url',
'boolean',
'number',
'integer',
'decimal',
'float',
'reference',
],
})
service.comparators.push({
title: 'Is not',
operator: '!=',
positive: false,
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
if (_.isArray(input)) {
return !_.some(input, function(i) {
return isEqual(i, mustMatchValue);
});
} else {
return !isEqual(input, mustMatchValue);
}
},
restrict: [
'string',
'email',
'url',
'boolean',
'number',
'integer',
'decimal',
'float',
'reference',
],
})
service.comparators.push({
title: 'Starts with',
operator: 'startswith',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
if (_.isArray(input)) {
return _.some(input, function(i) {
return _.startsWith(getString(i), getString(mustMatchValue))
});
} else {
return _.startsWith(getString(input), getString(mustMatchValue))
}
},
restrict: [
'string',
'email',
'url',
'reference',
],
})
service.comparators.push({
title: 'Ends with',
operator: 'endswith',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
if (_.isArray(input)) {
return _.some(input, function(i) {
return _.endsWith(getString(i), getString(mustMatchValue))
});
} else {
return _.endsWith(getString(input), getString(mustMatchValue))
}
},
restrict: [
'string',
'email',
'url',
'reference',
],
})
service.comparators.push({
title: 'Is similar to',
operator: 'like',
match(input, mustMatchValue, mustMatchValue2, options) {
if (_.isArray(input)) {
return _.some(input, function(i) {
return service.isSimilar(i, mustMatchValue);
});
} else {
return service.isSimilar(input, mustMatchValue);
}
},
restrict: ['string', 'url', 'email'],
})
service.comparators.push({
title: 'Contains characters',
operator: 'contains',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
if (_.isArray(input)) {
return _.some(input, function(i) {
return isContained(mustMatchValue, i);
});
} else {
return isContained(mustMatchValue, input);
}
},
restrict: ['string', 'url', 'email'],
})
service.comparators.push({
title: 'Does not contain characters',
operator: 'excludes',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
if (_.isArray(input)) {
return !_.some(input, function(i) {
return isContained(mustMatchValue, i);
});
} else {
return !isContained(mustMatchValue, input);
}
},
restrict: ['string', 'url', 'email'],
})
service.comparators.push({
title: 'Is greater than',
operator: '>',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
return parseFloat(input || 0) > parseFloat(mustMatchValue || 0);
},
restrict: [
'number',
'integer',
'decimal',
'float',
],
})
service.comparators.push({
title: 'Is less than',
operator: '<',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
if (isNotANumber(input)) {
return;
}
return parseFloat(input || 0) < parseFloat(mustMatchValue || 0);
},
restrict: [
'number',
'integer',
'decimal',
'float',
],
})
service.comparators.push({
title: 'Is not greater than',
operator: '!>',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
return !(parseFloat(input || 0) > parseFloat(mustMatchValue || 0));
},
restrict: [
'number',
'integer',
'decimal',
'float',
],
})
service.comparators.push({
title: 'Is not less than',
operator: '!<',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
return !(parseFloat(input || 0) < parseFloat(mustMatchValue || 0));
},
restrict: [
'number',
'integer',
'decimal',
'float',
],
})
service.comparators.push({
title: 'Is greater than or equal to',
operator: '>=',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
if (isNotANumber(input)) {
return;
}
return parseFloat(input || 0) >= parseFloat(mustMatchValue || 0);
},
restrict: [
'number',
'integer',
'decimal',
'float',
],
})
service.comparators.push({
title: 'Is less than or equal to',
operator: '<=',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
if (isNotANumber(input)) {
return;
}
return parseFloat(input || 0) <= parseFloat(mustMatchValue || 0);
},
restrict: [
'number',
'integer',
'decimal',
'float',
],
})
service.comparators.push({
title: 'Is between',
operator: 'between',
match(input, mustMatchValue1, mustMatchValue2, options) {
if (!options) {
options = {}
}
return isBetween(input, mustMatchValue1, mustMatchValue2);
},
restrict: [
'number',
'integer',
'decimal',
'float',
],
inputType: 'range',
})
service.comparators.push({
title: 'Is not between',
operator: 'notbetween',
match(input, mustMatchValue1, mustMatchValue2, options) {
if (!options) {
options = {}
}
return !isBetween(input, mustMatchValue1, mustMatchValue2);
},
restrict: [
'number',
'integer',
'decimal',
'float',
],
inputType: 'range',
})
service.comparators.push({
title: 'Is empty',
operator: 'empty',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
var result = isEmpty(input);
// ////console.log('CHECK IF IS EMPTY!!', result, input, mustMatchValue)
return result;
},
inputType: 'none',
})
service.comparators.push({
title: 'Is not empty',
operator: 'notempty',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
return !isEmpty(input);
},
inputType: 'none',
})
service.comparators.push({
title: 'Does not start with',
operator: 'doesnotstartwith',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
if (_.isArray(input)) {
return !_.some(input, function(i) {
return _.startsWith(getString(i), getString(mustMatchValue))
});
} else {
return !_.startsWith(getString(input), getString(mustMatchValue))
}
},
restrict: [
'string',
'email',
'url',
'reference',
],
})
service.comparators.push({
title: 'Does not end with',
operator: 'doesnotendwith',
match(input, mustMatchValue, NOT_USED, options) {
if (!options) {
options = {}
}
if (_.isArray(input)) {
return !_.some(input, function(i) {
return _.endsWith(getString(i), getString(mustMatchValue))
});
} else {
return !_.endsWith(getString(input), getString(mustMatchValue))
}
},
restrict: [
'string',
'email',
'url',
'reference',
],
})
var allTypes = [
'string',
'email',
'url',
'phone',
'date',
'boolean',
'number',
'integer',
'decimal',
'float',
'reference',
]
///////////////////////////////
service.comparatorLookup = {};
service.comparatorTypeLookup = {};
///////////////////////////////
//Loop through each available comparator
_.each(servi