UNPKG

3.61 kBJavaScriptView Raw
1// =============================================================================
2// Helper functions
3// =============================================================================
4import { ALL_WEEKDAYS } from './weekday';
5export var isPresent = function (value) {
6 return value !== null && value !== undefined;
7};
8export var isNumber = function (value) {
9 return typeof value === 'number';
10};
11export var isWeekdayStr = function (value) {
12 return ALL_WEEKDAYS.indexOf(value) >= 0;
13};
14export var isArray = Array.isArray;
15/**
16 * Simplified version of python's range()
17 */
18export var range = function (start, end) {
19 if (end === void 0) { end = start; }
20 if (arguments.length === 1) {
21 end = start;
22 start = 0;
23 }
24 var rang = [];
25 for (var i = start; i < end; i++)
26 rang.push(i);
27 return rang;
28};
29export var clone = function (array) {
30 return [].concat(array);
31};
32export var repeat = function (value, times) {
33 var i = 0;
34 var array = [];
35 if (isArray(value)) {
36 for (; i < times; i++)
37 array[i] = [].concat(value);
38 }
39 else {
40 for (; i < times; i++)
41 array[i] = value;
42 }
43 return array;
44};
45export var toArray = function (item) {
46 if (isArray(item)) {
47 return item;
48 }
49 return [item];
50};
51export function padStart(item, targetLength, padString) {
52 if (padString === void 0) { padString = ' '; }
53 var str = String(item);
54 targetLength = targetLength >> 0;
55 if (str.length > targetLength) {
56 return String(str);
57 }
58 targetLength = targetLength - str.length;
59 if (targetLength > padString.length) {
60 padString += repeat(padString, targetLength / padString.length);
61 }
62 return padString.slice(0, targetLength) + String(str);
63}
64/**
65 * Python like split
66 */
67export var split = function (str, sep, num) {
68 var splits = str.split(sep);
69 return num
70 ? splits.slice(0, num).concat([splits.slice(num).join(sep)])
71 : splits;
72};
73/**
74 * closure/goog/math/math.js:modulo
75 * Copyright 2006 The Closure Library Authors.
76 * The % operator in JavaScript returns the remainder of a / b, but differs from
77 * some other languages in that the result will have the same sign as the
78 * dividend. For example, -1 % 8 == -1, whereas in some other languages
79 * (such as Python) the result would be 7. This function emulates the more
80 * correct modulo behavior, which is useful for certain applications such as
81 * calculating an offset index in a circular list.
82 *
83 * @param {number} a The dividend.
84 * @param {number} b The divisor.
85 * @return {number} a % b where the result is between 0 and b (either 0 <= x < b
86 * or b < x <= 0, depending on the sign of b).
87 */
88export var pymod = function (a, b) {
89 var r = a % b;
90 // If r and b differ in sign, add b to wrap the result to the correct sign.
91 return r * b < 0 ? r + b : r;
92};
93/**
94 * @see: <http://docs.python.org/library/functions.html#divmod>
95 */
96export var divmod = function (a, b) {
97 return { div: Math.floor(a / b), mod: pymod(a, b) };
98};
99export var empty = function (obj) {
100 return !isPresent(obj) || obj.length === 0;
101};
102/**
103 * Python-like boolean
104 * @return {Boolean} value of an object/primitive, taking into account
105 * the fact that in Python an empty list's/tuple's
106 * boolean value is False, whereas in JS it's true
107 */
108export var notEmpty = function (obj) {
109 return !empty(obj);
110};
111/**
112 * Return true if a value is in an array
113 */
114export var includes = function (arr, val) {
115 return notEmpty(arr) && arr.indexOf(val) !== -1;
116};
117//# sourceMappingURL=helpers.js.map
\No newline at end of file