-
Notifications
You must be signed in to change notification settings - Fork 0
/
datedash.js
451 lines (412 loc) · 10.4 KB
/
datedash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/**
* @license
* datedash <https://github.com/flavioespinoza/datedash>
* Inspired by lodash.js <https://lodash.com/> by John-David Dalton <https://github.com/jdalton>
*/
(function () {
/** Used as the semantic version number. */
const __package = require("./package.json");
const VERSION = __package.version;
/** Detect free variable `global` from Node.js. */
const freeGlobal =
typeof global == "object" &&
global &&
global.Object === Object &&
global;
/** Detect free variable `self`. */
const freeSelf =
typeof self == "object" && self && self.Object === Object && self;
/** Used as a reference to the global object. */
const root = freeGlobal || freeSelf || Function("return this")();
/** Detect free variable `exports`. */
const freeExports =
typeof exports == "object" && exports && !exports.nodeType && exports;
/** Detect free variable `module`. */
const freeModule =
freeExports &&
typeof module == "object" &&
module &&
!module.nodeType &&
module;
/*--------------------------------------------------------------------------*/
/** Built-in value references. */
const Symbol = root.Symbol;
/** Used to lookup non-minified function names. */
const realNames = {};
/*------------------------------------------------------------------------*/
// Begin Methods
/*------------------------------------------------------------------------*/
/**
* @private
* @name _d
* @constructor
* @category Seq
* @param {*} value The value to wrap in a `datedash` instance.
* @returns {Object} Returns the new `datedash` wrapper instance.
*/
function datedash() {
// No operation performed.
}
/*------------------------------------------------------------------------*/
/**
* Import using `esm` or `TypeScript`
*
* @static
* @since 1.0.0
* @category Import
* @example
*
* import _d from 'datedash'
*
* _d.date('3/14/2019', 'uk')
* // => 14 Mar 2019
*
* _d.addDays('3/6/19', 1, '-')
* // => 03-07-2019
*
* _d.subtractDays('3/6/19', 1, '-')
* // => 03-05-2019
*/
this.import = () => {
// No operation performed.
// Required for doc generation
};
/**
* Import individual ES Modules using `esm` or `TypeScript`
*
* @static
* @since 1.0.0
* @category Import
* @example
*
* import { addDays, subtractDays } from 'datedash'
*
* addDays('3/6/19', 1, '-')
* // => 03-07-2019
*
* subtractDays('3/6/19', 1, '-')
* // => 03-05-2019
*/
this.importModules = () => {
// No operation performed.
// Required for doc generation
};
/*------------------------------------------------------------------------*/
/**
* Use require
*
* @static
* @since 1.0.0
* @category Require
* @example
*
* const _d = require('datedash')
*
* _d.date('3/14/2019', 'uk')
* // => 14 Mar 2019
*
* _d.addDays('3/6/19', 1, '-')
* // => 03-07-2019
*
* _d.subtractDays('3/6/19', 1, '-')
* // => 03-05-2019
*/
this.require = () => {};
/**
*
* Computes input `date` converts to string and returns with specified `format`.
*
* @static
* @memberOf _d
* @since 1.0.0
* @category Date
* @param {date} Date options are `new Date()`, `timestamp` or `string` in valid date format. See example below.
* @param {string} format
* @returns {string} Returns the date as a String in specified format.
* @example
*
* let any_date = "1/07/2019"
*
* _d.date(any_date, '/')
* // => 01/07/2019
*
* _d.date(any_date, '-')
* // => 01-07-2019
*
* _d.date(any_date, '.')
* // => 01.07.2019
*
* _d.date(any_date, 'MMM DD YYYY')
* // => Jan 07 2019
*
* _d.date(any_date, 'england')
* // => 07 Jan 2019
*
* _d.date(any_date, 'uk')
* // => 07 Jan 2019
*
* _d.date(any_date, 'full')
* // => Mon Jan 07 2019 00:00:00 GMT-0700 (Mountain Standard Time)
*
*/
const date = require("./methods/date");
/**
* Gets the timestamp of the number of milliseconds that have elapsed since
* the Unix epoch (1 January 1970 00:00:00 UTC).
*
* @static
* @memberOf _d
* @since 1.0.0
* @category Date
* @returns {number} Returns the timestamp.
* @example
*
* const { defer } = require('lodash')
*
* defer(function(stamp) {
* console.log(_d.now() - stamp)
* }, _d.now())
*
* // => Logs milliseconds it took for the deferred invocation.
*/
const now = require("./methods/now");
/**
* Gets the timestamp of the number of milliseconds that have elapsed since
* `date` argument. If `date` is `undefined` it gives milliseconds elapsed since
* the Unix epoch (1 January 1970 00:00:00 UTC).
*
* @static
* @memberOf _d
* @since 1.0.0
* @category Date
* @param {date} Date to convert to timestamp.
* @returns {number} Returns the timestamp.
* @example
*
* _d.getTimestamp('July 4 1776')
* // => 121244400000
*
* _d.getTimestamp()
* // => 1552353178563
* // returns now timestamp
*
* _d.getTimestamp('11/4/1973')
* // => -6106035604000
*/
const getTimestamp = require("./methods/getTimestamp");
/**
* Verifies if `value` is a valid `Date object` and valid `Date`.
*
* @static
* @memberOf _d
* @since 1.0.0
* @category Date
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is a `Date object` & valid `Date`, else `false`.
* @example
*
* _d.isDate('3/3/19')
* // => true
*
* _d.isDate(new Date())
* // => true
*
* _d.isDate('Jul 4 1776')
* // => true
*
* _d.isDate(25200000)
* // => true
*
* _d.isDate('3/33/19')
* // => false
*
* function getDate() {
* return '1/1/19'
* }
* _d.isDate(getDate)
* // => false
*
* _d.isDate(getDate())
* // => true
*/
const isDate = require("./methods/isDate");
/**
* Input `_date` add `nHours` with `format`
*
* @static
* @memberOf _d
* @since 1.0.50
* @category Math
* @param {date} Date
* @param {number} hours Number of hours to add
* @param {string} format 'ts', 'timestamp', 'full', `undefined`, `null`
* @returns {date} 'ts' or 'timestamp' return numeric timestamp; 'full', `null`, or `undefined` return a complete Date object
* @example
*
* let now_date_full = new Date()
* // => Tue Jun 18 2019 23:17:10 GMT-0600 (Mountain Daylight Time)
* let now_date_ts = _d.getTimestamp(now_date_full)
* // => 1560921430024
*
* // Add 1hr return full date object
* let add_1hr = _d.addHours(now_date_full, 1, 'full')
* // => Wed Jun 19 2019 00:17:10 GMT-0600 (Mountain Daylight Time)
*
* // Add 1hr return timestamp
* let add_1hr_ts = _d.addHours(now_date_full, 1, 'ts')
* // => 1560925030024
*/
const addHours = require("./methods/addHours");
/**
* Input `_date` subtract `nHours` with `format`
*
* @static
* @memberOf _d
* @since 1.0.50
* @category Math
* @param {date} Date
* @param {number} hours to subtract
* @param {string} String "full", null or undefined returns a date object 'Tue Jun 18 2019 22:37:29 GMT-0600 (Mountain Daylight Time)'; "ts" or "timestamp" returns the date as a timestamp 1560919049590
* @example
*
* let now_date_full = new Date()
* // => Tue Jun 18 2019 23:23:30 GMT-0600 (Mountain Daylight Time)
* let now_date_ts = _d.getTimestamp(now_date_full)
* // => 1560921810079
*
* // Subtract 1hr return full date object
* let add_1hr = _d.subtractHours(now_date_full, 1, 'full')
* // => Tue Jun 18 2019 22:23:30 GMT-0600 (Mountain Daylight Time)
*
* // Subtract 1hr return timestamp
* let add_1hr_ts = _d.subtractHours(now_date_full, 1, 'ts')
* // => 1560918210079
*/
const subtractHours = require("./methods/subtractHours");
/**
* Input `_date` add `nDays` with `format`
*
* @static
* @memberOf _d
* @since 1.0.0
* @category Math
* @param {date} Date
* @param {number} days to add
* @param {string} format 'ts', 'timestamp', 'full', `undefined`, `null`
* @returns {date} 'ts' or 'timestamp' return numeric timestamp; 'full', `null`, or `undefined` return a complete Date object
* @example
*
* const any_date = '3/6/19'
*
* _d.addDays(any_date, 1, '-')
* // => 03-07-2019
*
* _d.addDays(any_date, 2, '.')
* // => 03.08.2019
*
* _d.addDays(any_date, 3, 'uk')
* // => 09 Mar 2019
*/
const addDays = require("./methods/addDays");
/**
* Input `_date` subtract `nDays` with `format`
*
* @static
* @memberOf _d
* @since 1.0.0
* @category Math
* @param {Date} Date
* @param {number} days to subtract
* @param {string} format
* @example
*
* const any_date = '3/6/19'
*
* _d.subtractDays(any_date, 1, '-')
* // => 03-05-2019
*
* _d.subtractDays(any_date, 2, '.')
* // => 03.04.2019
*
* _d.subtractDays(any_date, 3, 'uk')
* // => 03 Mar 2019
*/
const subtractDays = require("./methods/subtractDays");
/*------------------------------------------------------------------------*/
/**
* Test of: `yarn add https://github.com/flavioespinoza/datedash.git#master`
*
* @static
* @since 1.0.0
* @category Yarn
* @example
*
* import _d from 'datedash'
*
* _d.yarnTest()
*
*/
const yarnTest = () => {
console.log("yarn_test SUCCESS!");
return {
val: "yarn_test",
key: "Yarn Test Success",
};
};
/*------------------------------------------------------------------------*/
/**
* Test of: `yarn add https://github.com/flavioespinoza/datedash.git#master`
*
* @static
* @since 1.0.0
* @category Yarn
* @example
*
* import _d from 'datedash'
*
* _d.yarnTest2()
*
*/
const yarnTest2 = () => {
console.log("yarnTest2 SUCCESS!");
return {
val: "yarn_test_2",
key: "Yarn Test 2 Success",
date: new Date(),
};
};
/*------------------------------------------------------------------------*/
// Date
datedash.date = date;
datedash.now = now;
datedash.getTimestamp = getTimestamp;
datedash.isDate = isDate;
// Yarn
datedash.yarnTest = yarnTest;
datedash.yarnTest2 = yarnTest2;
// Math Hours
datedash.addHours = addHours;
datedash.subtractHours = subtractHours;
// Math Days
datedash.addDays = addDays;
datedash.subtractDays = subtractDays;
/*------------------------------------------------------------------------*/
/**
* The semantic version number.
* @private
* @static
* @memberOf _d
* @type {string}
*/
datedash.VERSION = VERSION;
/*--------------------------------------------------------------------------*/
if (freeModule) {
// Export for Node.js.
(freeModule.exports = datedash)._d = datedash;
// Export for CommonJS support.
freeExports._d = datedash;
} else {
// Export to the global object.
root._d = datedash;
}
}.call(this));