1
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
export default function absCeil(number) {
|
||||
if (number < 0) {
|
||||
return Math.floor(number);
|
||||
} else {
|
||||
return Math.ceil(number);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
export default function absFloor(number) {
|
||||
if (number < 0) {
|
||||
// -0 -> 0
|
||||
return Math.ceil(number) || 0;
|
||||
} else {
|
||||
return Math.floor(number);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
export default function absRound(number) {
|
||||
if (number < 0) {
|
||||
return Math.round(-1 * number) * -1;
|
||||
} else {
|
||||
return Math.round(number);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import toInt from './to-int';
|
||||
|
||||
// compare two arrays, return the number of differences
|
||||
export default function compareArrays(array1, array2, dontConvert) {
|
||||
var len = Math.min(array1.length, array2.length),
|
||||
lengthDiff = Math.abs(array1.length - array2.length),
|
||||
diffs = 0,
|
||||
i;
|
||||
for (i = 0; i < len; i++) {
|
||||
if (
|
||||
(dontConvert && array1[i] !== array2[i]) ||
|
||||
(!dontConvert && toInt(array1[i]) !== toInt(array2[i]))
|
||||
) {
|
||||
diffs++;
|
||||
}
|
||||
}
|
||||
return diffs + lengthDiff;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// Pick the first defined of two or three arguments.
|
||||
export default function defaults(a, b, c) {
|
||||
if (a != null) {
|
||||
return a;
|
||||
}
|
||||
if (b != null) {
|
||||
return b;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import extend from './extend';
|
||||
import { hooks } from './hooks';
|
||||
import hasOwnProp from './has-own-prop';
|
||||
|
||||
function warn(msg) {
|
||||
if (
|
||||
hooks.suppressDeprecationWarnings === false &&
|
||||
typeof console !== 'undefined' &&
|
||||
console.warn
|
||||
) {
|
||||
console.warn('Deprecation warning: ' + msg);
|
||||
}
|
||||
}
|
||||
|
||||
export function deprecate(msg, fn) {
|
||||
var firstTime = true;
|
||||
|
||||
return extend(function () {
|
||||
if (hooks.deprecationHandler != null) {
|
||||
hooks.deprecationHandler(null, msg);
|
||||
}
|
||||
if (firstTime) {
|
||||
var args = [],
|
||||
arg,
|
||||
i,
|
||||
key,
|
||||
argLen = arguments.length;
|
||||
for (i = 0; i < argLen; i++) {
|
||||
arg = '';
|
||||
if (typeof arguments[i] === 'object') {
|
||||
arg += '\n[' + i + '] ';
|
||||
for (key in arguments[0]) {
|
||||
if (hasOwnProp(arguments[0], key)) {
|
||||
arg += key + ': ' + arguments[0][key] + ', ';
|
||||
}
|
||||
}
|
||||
arg = arg.slice(0, -2); // Remove trailing comma and space
|
||||
} else {
|
||||
arg = arguments[i];
|
||||
}
|
||||
args.push(arg);
|
||||
}
|
||||
warn(
|
||||
msg +
|
||||
'\nArguments: ' +
|
||||
Array.prototype.slice.call(args).join('') +
|
||||
'\n' +
|
||||
new Error().stack
|
||||
);
|
||||
firstTime = false;
|
||||
}
|
||||
return fn.apply(this, arguments);
|
||||
}, fn);
|
||||
}
|
||||
|
||||
var deprecations = {};
|
||||
|
||||
export function deprecateSimple(name, msg) {
|
||||
if (hooks.deprecationHandler != null) {
|
||||
hooks.deprecationHandler(name, msg);
|
||||
}
|
||||
if (!deprecations[name]) {
|
||||
warn(msg);
|
||||
deprecations[name] = true;
|
||||
}
|
||||
}
|
||||
|
||||
hooks.suppressDeprecationWarnings = false;
|
||||
hooks.deprecationHandler = null;
|
||||
@@ -0,0 +1,19 @@
|
||||
import hasOwnProp from './has-own-prop';
|
||||
|
||||
export default function extend(a, b) {
|
||||
for (var i in b) {
|
||||
if (hasOwnProp(b, i)) {
|
||||
a[i] = b[i];
|
||||
}
|
||||
}
|
||||
|
||||
if (hasOwnProp(b, 'toString')) {
|
||||
a.toString = b.toString;
|
||||
}
|
||||
|
||||
if (hasOwnProp(b, 'valueOf')) {
|
||||
a.valueOf = b.valueOf;
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export default function hasOwnProp(a, b) {
|
||||
return Object.prototype.hasOwnProperty.call(a, b);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
export { hooks, setHookCallback };
|
||||
|
||||
var hookCallback;
|
||||
|
||||
function hooks() {
|
||||
return hookCallback.apply(null, arguments);
|
||||
}
|
||||
|
||||
// This is done to register the method called with moment()
|
||||
// without creating circular dependencies.
|
||||
function setHookCallback(callback) {
|
||||
hookCallback = callback;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
var indexOf;
|
||||
|
||||
if (Array.prototype.indexOf) {
|
||||
indexOf = Array.prototype.indexOf;
|
||||
} else {
|
||||
indexOf = function (o) {
|
||||
// I know
|
||||
var i;
|
||||
for (i = 0; i < this.length; ++i) {
|
||||
if (this[i] === o) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
}
|
||||
|
||||
export { indexOf as default };
|
||||
@@ -0,0 +1,6 @@
|
||||
export default function isArray(input) {
|
||||
return (
|
||||
input instanceof Array ||
|
||||
Object.prototype.toString.call(input) === '[object Array]'
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import isObjectEmpty from './is-object-empty';
|
||||
import hasOwnProp from './has-own-prop';
|
||||
import isObject from './is-object';
|
||||
|
||||
export default function isCalendarSpec(input) {
|
||||
var objectTest = isObject(input) && !isObjectEmpty(input),
|
||||
propertyTest = false,
|
||||
properties = [
|
||||
'sameDay',
|
||||
'nextDay',
|
||||
'lastDay',
|
||||
'nextWeek',
|
||||
'lastWeek',
|
||||
'sameElse',
|
||||
],
|
||||
i,
|
||||
property;
|
||||
|
||||
for (i = 0; i < properties.length; i += 1) {
|
||||
property = properties[i];
|
||||
propertyTest = propertyTest || hasOwnProp(input, property);
|
||||
}
|
||||
|
||||
return objectTest && propertyTest;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
export default function isDate(input) {
|
||||
return (
|
||||
input instanceof Date ||
|
||||
Object.prototype.toString.call(input) === '[object Date]'
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
export default function isFunction(input) {
|
||||
return (
|
||||
(typeof Function !== 'undefined' && input instanceof Function) ||
|
||||
Object.prototype.toString.call(input) === '[object Function]'
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export function isLeapYear(year) {
|
||||
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import isObjectEmpty from './is-object-empty';
|
||||
import hasOwnProp from './has-own-prop';
|
||||
import isObject from './is-object';
|
||||
import isDate from './is-date';
|
||||
import isNumber from './is-number';
|
||||
import isString from './is-string';
|
||||
import { isMoment } from '../moment/constructor';
|
||||
import isArray from './is-array';
|
||||
|
||||
// type MomentInput = Moment | Date | string | number | (number | string)[] | MomentInputObject | void; // null | undefined
|
||||
export function isMomentInput(input) {
|
||||
return (
|
||||
isMoment(input) ||
|
||||
isDate(input) ||
|
||||
isString(input) ||
|
||||
isNumber(input) ||
|
||||
isNumberOrStringArray(input) ||
|
||||
isMomentInputObject(input) ||
|
||||
input === null ||
|
||||
input === undefined
|
||||
);
|
||||
}
|
||||
|
||||
export function isMomentInputObject(input) {
|
||||
var objectTest = isObject(input) && !isObjectEmpty(input),
|
||||
propertyTest = false,
|
||||
properties = [
|
||||
'years',
|
||||
'year',
|
||||
'y',
|
||||
'months',
|
||||
'month',
|
||||
'M',
|
||||
'days',
|
||||
'day',
|
||||
'd',
|
||||
'dates',
|
||||
'date',
|
||||
'D',
|
||||
'hours',
|
||||
'hour',
|
||||
'h',
|
||||
'minutes',
|
||||
'minute',
|
||||
'm',
|
||||
'seconds',
|
||||
'second',
|
||||
's',
|
||||
'milliseconds',
|
||||
'millisecond',
|
||||
'ms',
|
||||
],
|
||||
i,
|
||||
property,
|
||||
propertyLen = properties.length;
|
||||
|
||||
for (i = 0; i < propertyLen; i += 1) {
|
||||
property = properties[i];
|
||||
propertyTest = propertyTest || hasOwnProp(input, property);
|
||||
}
|
||||
|
||||
return objectTest && propertyTest;
|
||||
}
|
||||
|
||||
function isNumberOrStringArray(input) {
|
||||
var arrayTest = isArray(input),
|
||||
dataTypeTest = false;
|
||||
if (arrayTest) {
|
||||
dataTypeTest =
|
||||
input.filter(function (item) {
|
||||
return !isNumber(item) && isString(input);
|
||||
}).length === 0;
|
||||
}
|
||||
return arrayTest && dataTypeTest;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
export default function isNumber(input) {
|
||||
return (
|
||||
typeof input === 'number' ||
|
||||
Object.prototype.toString.call(input) === '[object Number]'
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import hasOwnProp from './has-own-prop';
|
||||
|
||||
export default function isObjectEmpty(obj) {
|
||||
if (Object.getOwnPropertyNames) {
|
||||
return Object.getOwnPropertyNames(obj).length === 0;
|
||||
} else {
|
||||
var k;
|
||||
for (k in obj) {
|
||||
if (hasOwnProp(obj, k)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
export default function isObject(input) {
|
||||
// IE8 will treat undefined and null as object if it wasn't for
|
||||
// input != null
|
||||
return (
|
||||
input != null &&
|
||||
Object.prototype.toString.call(input) === '[object Object]'
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export default function isString(input) {
|
||||
return typeof input === 'string' || input instanceof String;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export default function isUndefined(input) {
|
||||
return input === void 0;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import hasOwnProp from './has-own-prop';
|
||||
|
||||
var keys;
|
||||
|
||||
if (Object.keys) {
|
||||
keys = Object.keys;
|
||||
} else {
|
||||
keys = function (obj) {
|
||||
var i,
|
||||
res = [];
|
||||
for (i in obj) {
|
||||
if (hasOwnProp(obj, i)) {
|
||||
res.push(i);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
};
|
||||
}
|
||||
|
||||
export { keys as default };
|
||||
@@ -0,0 +1,9 @@
|
||||
export default function map(arr, fn) {
|
||||
var res = [],
|
||||
i,
|
||||
arrLen = arr.length;
|
||||
for (i = 0; i < arrLen; ++i) {
|
||||
res.push(fn(arr[i], i));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export default function mod(n, x) {
|
||||
return ((n % x) + x) % x;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
var some;
|
||||
if (Array.prototype.some) {
|
||||
some = Array.prototype.some;
|
||||
} else {
|
||||
some = function (fun) {
|
||||
var t = Object(this),
|
||||
len = t.length >>> 0,
|
||||
i;
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
if (i in t && fun.call(this, t[i], i, t)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
export { some as default };
|
||||
@@ -0,0 +1,12 @@
|
||||
import absFloor from './abs-floor';
|
||||
|
||||
export default function toInt(argumentForCoercion) {
|
||||
var coercedNumber = +argumentForCoercion,
|
||||
value = 0;
|
||||
|
||||
if (coercedNumber !== 0 && isFinite(coercedNumber)) {
|
||||
value = absFloor(coercedNumber);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
export default function zeroFill(number, targetLength, forceSign) {
|
||||
var absNumber = '' + Math.abs(number),
|
||||
zerosToFill = targetLength - absNumber.length,
|
||||
sign = number >= 0;
|
||||
return (
|
||||
(sign ? (forceSign ? '+' : '') : '-') +
|
||||
Math.pow(10, Math.max(0, zerosToFill)).toString().substr(1) +
|
||||
absNumber
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user