JSFiddle - React, Tailwind, and code Playground
by tonytlwu
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/sifter/0.5.3/sifter.min.js"></script>
JavaScript
// https://raw.githubusercontent.com/Fliplet/sift.js/master/sift.js
(function() {
'use strict';
/**
*/
function isFunction(value) {
return typeof value === 'function';
}
/**
*/
function isArray(value) {
return Object.prototype.toString.call(value) === '[object Array]';
}
/**
*/
function comparable(value) {
if (value instanceof Date) {
return value.getTime();
} else if (isArray(value)) {
return value.map(comparable);
} else if (value && typeof value.toJSON === 'function') {
return value.toJSON();
} else {
return value;
}
}
/**
* Given a value, most of the times would be a string try to return
* a value to compare. It will work for things like:
* "-$1", "01-01-2018", "1 dollar"
* Return NaN if we can't get something
* @param {*} value - Value to compare
*/
function flComparable(value) {
var result;
if (typeof value === 'number') {
return value;
}
if (typeof value === 'string') {
// Try to parse is as a Date
var validDateFormat = /([0-9]{2,4})([-.])([0-9]{2})\2([0-9]{2,4})/;
if (value.match(validDateFormat)) {
var date = new Date(value);
return result = date.getTime();
}
// Try to parse float
result = parseFloat(value.replace(/[^0-9,\.-]+/, ''));
}
return result;
}
function isComparable(a, b) {
return !(isNaN(a) && isNaN(b));;
}
function get(obj, key) {
return isFunction(obj.get) ? obj.get(key) : obj[key];
}
/**
*/
function or(validator) {
return function(a, b) {
if (!isArray(b) || !b.length) {
return validator(a, b);
}
for (var i = 0, n = b.length; i < n; i++) {
if (validator(a, get(b,i))) return true;
}
return false;
}
}
/**
*/
function and(validator) {
return function(a, b) {
if (!isArray(b) || !b.length) {
return validator(a, b);
}
for (var i = 0,...