UNPKG

1.66 kBJavaScriptView Raw
1Object.defineProperty(Function.prototype, 'orElse', {
2 writable: true,
3 enumerable: false,
4 value: function(b) {
5 var a = this;
6 return function() {
7 try {
8 return a.call(this, arguments);
9 } catch (e) {
10 if (e instanceof TypeError && e.message === 'No match') {
11 return b.call(this, arguments);
12 } else {
13 throw e;
14 }
15 }
16 };
17 }
18});
19
20Date.unapply = function(date) {
21 if (Object.prototype.toString.call(date) === '[object Date]') {
22 return [
23 date.getFullYear(),
24 date.getMonth(),
25 date.getDate(),
26 date.getHours(),
27 date.getMinutes(),
28 date.getSeconds(),
29 date.getMilliseconds()
30 ];
31 }
32};
33
34Date.unapplyObject = function(date) {
35 if (Object.prototype.toString.call(date) === '[object Date]') {
36 return {
37 year: date.getFullYear(),
38 month: date.getMonth(),
39 date: date.getDate(),
40 hours: date.getHours(),
41 minutes: date.getMinutes(),
42 seconds: date.getSeconds(),
43 milliseconds: date.getMilliseconds(),
44 time: date.getTime()
45 };
46 }
47};
48
49function regExpFlags(reg) {
50 var flags = {};
51 if (reg.global) flags['g'] = true;
52 if (reg.ignoreCase) flags['i'] = true;
53 if (reg.multiline) flags['m'] = true;
54 if (reg.sticky) flags['y'] = true;
55 return flags;
56}
57
58RegExp.unapply = function(reg) {
59 if (Object.prototype.toString.call(reg) === '[object RegExp]') {
60 return [reg.source, regExpFlags(reg)];
61 }
62};
63
64RegExp.unapplyObject = function(reg) {
65 if (Object.prototype.toString.call(reg) === '[object RegExp]') {
66 return {
67 pattern: reg.source,
68 flags: regExpFlags(reg)
69 };
70 }
71};