@bbn/bbn 2.0.21 → 2.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/date.d.ts CHANGED
@@ -22,6 +22,11 @@ declare class bbnDateTool {
22
22
  weekdaysLong?: string[];
23
23
  weekdaysShort?: string[];
24
24
  }): Date;
25
+ /**
26
+ * Compare 2 dates with a given precision.
27
+ * @returns -1 if this < other, 0 if equal, 1 if this > other
28
+ */
29
+ static compare(date1: any, date2: any, unit?: string): -1 | 0 | 1;
25
30
  constructor(value: any, inputFormat?: null | String);
26
31
  parse(input: string, format: string): bbnDateTool;
27
32
  matchFormat(value: any, format: string): boolean;
@@ -63,11 +68,6 @@ declare class bbnDateTool {
63
68
  get S(): string;
64
69
  get WW(): string;
65
70
  get isValid(): boolean;
66
- inLeapYear(): boolean;
67
- daysInMonth(): number;
68
- valueOf(): number;
69
- add(value: number, unit?: string): bbnDateTool | null;
70
- subtract(value: number, unit?: string): bbnDateTool;
71
71
  dateFromFormat(value: string, unit: string | null): Date;
72
72
  date(v?: any): string | bbnDateTool;
73
73
  datetime(v?: any): string | bbnDateTool;
@@ -77,11 +77,16 @@ declare class bbnDateTool {
77
77
  format(format?: string): string;
78
78
  unix(ms?: boolean | number): number | bbnDateTool;
79
79
  sql(noTime?: boolean): string;
80
+ inLeapYear(): boolean;
81
+ daysInMonth(): number;
82
+ valueOf(): number;
80
83
  /**
81
84
  * Compare this date to another date with a given precision.
82
85
  * @returns -1 if this < other, 0 if equal, 1 if this > other
83
86
  */
84
87
  compare(date: any, unit?: string): -1 | 0 | 1;
88
+ add(value: number, unit?: string): bbnDateTool | null;
89
+ subtract(value: number, unit?: string): bbnDateTool;
85
90
  isBefore(date: any, unit?: string): Boolean;
86
91
  isAfter(date: any, unit?: string): Boolean;
87
92
  isSame(date: any, unit?: string): Boolean;
package/dist/date.js CHANGED
@@ -779,6 +779,44 @@ class bbnDateTool {
779
779
  }
780
780
  return date;
781
781
  }
782
+ /**
783
+ * Compare 2 dates with a given precision.
784
+ * @returns -1 if this < other, 0 if equal, 1 if this > other
785
+ */
786
+ static compare(date1, date2, unit = '') {
787
+ var _a;
788
+ const d1 = date1 instanceof bbnDateTool ? date1 : new bbnDateTool(date1);
789
+ const d2 = date2 instanceof bbnDateTool ? date2 : new bbnDateTool(date2);
790
+ const realUnit = unitsCorrespondence[unit] || null;
791
+ // If no unit or unknown unit, fall back to timestamp comparison
792
+ if (!realUnit) {
793
+ if (d1.mtst < d2.mtst) {
794
+ return -1;
795
+ }
796
+ if (d1.mtst > d2.mtst) {
797
+ return 1;
798
+ }
799
+ return 0;
800
+ }
801
+ const order = ['y', 'm', 'd', 'h', 'i', 's'];
802
+ // Compare step by step until the requested precision
803
+ for (const u of order) {
804
+ const key = (_a = getRow(units, un => un[0] === u)) === null || _a === void 0 ? void 0 : _a[1];
805
+ const a = d2[key]();
806
+ const b = d1[key]();
807
+ if (a < b) {
808
+ return -1;
809
+ }
810
+ if (a > b) {
811
+ return 1;
812
+ }
813
+ // Stop when we've reached the desired unit
814
+ if (u === realUnit) {
815
+ break;
816
+ }
817
+ }
818
+ return 0;
819
+ }
782
820
  constructor(value, inputFormat = null) {
783
821
  _bbnDateTool_value.set(this, void 0);
784
822
  _bbnDateTool_isDuration.set(this, false);
@@ -1038,52 +1076,6 @@ class bbnDateTool {
1038
1076
  get isValid() {
1039
1077
  return __classPrivateFieldGet(this, _bbnDateTool_value, "f") !== undefined;
1040
1078
  }
1041
- inLeapYear() {
1042
- if (this.isValid) {
1043
- const year = __classPrivateFieldGet(this, _bbnDateTool_value, "f").getFullYear();
1044
- return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
1045
- }
1046
- return false;
1047
- }
1048
- daysInMonth() {
1049
- if (this.isValid) {
1050
- switch (__classPrivateFieldGet(this, _bbnDateTool_value, "f").getMonth()) {
1051
- case 1:
1052
- if (__classPrivateFieldGet(this, _bbnDateTool_value, "f").getFullYear() % 4 === 0) {
1053
- return 29;
1054
- }
1055
- else {
1056
- return 28;
1057
- }
1058
- case 0:
1059
- case 3:
1060
- case 5:
1061
- case 8:
1062
- case 10:
1063
- return 30;
1064
- default:
1065
- return 31;
1066
- }
1067
- }
1068
- }
1069
- valueOf() {
1070
- return this.mtst;
1071
- }
1072
- add(value, unit = 'd') {
1073
- const date = __classPrivateFieldGet(this, _bbnDateTool_value, "f") ? new Date(__classPrivateFieldGet(this, _bbnDateTool_value, "f").getTime()) : new Date();
1074
- if (unitsCorrespondence[unit]) {
1075
- const realUnit = unitsCorrespondence[unit];
1076
- const suffix = realUnit === 'y' ? 'FullYear' : unitsMap[realUnit];
1077
- const getter = 'get' + suffix;
1078
- const setter = 'set' + suffix;
1079
- date[setter](date[getter]() + value);
1080
- return new bbnDateTool(date);
1081
- }
1082
- return null;
1083
- }
1084
- subtract(value, unit) {
1085
- return this.add(-value, unit);
1086
- }
1087
1079
  dateFromFormat(value, unit) {
1088
1080
  const d = new Date();
1089
1081
  return d;
@@ -1187,42 +1179,58 @@ class bbnDateTool {
1187
1179
  ? this.format('YYYY-MM-DD')
1188
1180
  : this.format('YYYY-MM-DD HH:II:SS');
1189
1181
  }
1182
+ inLeapYear() {
1183
+ if (this.isValid) {
1184
+ const year = __classPrivateFieldGet(this, _bbnDateTool_value, "f").getFullYear();
1185
+ return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
1186
+ }
1187
+ return false;
1188
+ }
1189
+ daysInMonth() {
1190
+ if (this.isValid) {
1191
+ switch (__classPrivateFieldGet(this, _bbnDateTool_value, "f").getMonth()) {
1192
+ case 1:
1193
+ if (__classPrivateFieldGet(this, _bbnDateTool_value, "f").getFullYear() % 4 === 0) {
1194
+ return 29;
1195
+ }
1196
+ else {
1197
+ return 28;
1198
+ }
1199
+ case 0:
1200
+ case 3:
1201
+ case 5:
1202
+ case 8:
1203
+ case 10:
1204
+ return 30;
1205
+ default:
1206
+ return 31;
1207
+ }
1208
+ }
1209
+ }
1210
+ valueOf() {
1211
+ return this.mtst;
1212
+ }
1190
1213
  /**
1191
1214
  * Compare this date to another date with a given precision.
1192
1215
  * @returns -1 if this < other, 0 if equal, 1 if this > other
1193
1216
  */
1194
1217
  compare(date, unit = '') {
1195
- var _a;
1196
- const d = date instanceof bbnDateTool ? date : new bbnDateTool(date);
1197
- const realUnit = unitsCorrespondence[unit] || null;
1198
- // If no unit or unknown unit, fall back to timestamp comparison
1199
- if (!realUnit) {
1200
- if (this.mtst < d.mtst) {
1201
- return -1;
1202
- }
1203
- if (this.mtst > d.mtst) {
1204
- return 1;
1205
- }
1206
- return 0;
1207
- }
1208
- const order = ['y', 'm', 'd', 'h', 'i', 's'];
1209
- // Compare step by step until the requested precision
1210
- for (const u of order) {
1211
- const key = (_a = getRow(units, un => un[0] === u)) === null || _a === void 0 ? void 0 : _a[1];
1212
- const a = this[key]();
1213
- const b = d[key]();
1214
- if (a < b) {
1215
- return -1;
1216
- }
1217
- if (a > b) {
1218
- return 1;
1219
- }
1220
- // Stop when we've reached the desired unit
1221
- if (u === realUnit) {
1222
- break;
1223
- }
1218
+ return bbnDateTool.compare(this, date, unit);
1219
+ }
1220
+ add(value, unit = 'd') {
1221
+ const date = __classPrivateFieldGet(this, _bbnDateTool_value, "f") ? new Date(__classPrivateFieldGet(this, _bbnDateTool_value, "f").getTime()) : new Date();
1222
+ if (unitsCorrespondence[unit]) {
1223
+ const realUnit = unitsCorrespondence[unit];
1224
+ const suffix = realUnit === 'y' ? 'FullYear' : unitsMap[realUnit];
1225
+ const getter = 'get' + suffix;
1226
+ const setter = 'set' + suffix;
1227
+ date[setter](date[getter]() + value);
1228
+ return new bbnDateTool(date);
1224
1229
  }
1225
- return 0;
1230
+ return null;
1231
+ }
1232
+ subtract(value, unit) {
1233
+ return this.add(-value, unit);
1226
1234
  }
1227
1235
  isBefore(date, unit = '') {
1228
1236
  return this.compare(date, unit) === -1;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Returns a new array generated by the execution of a function for each item of the given array.
3
+ *
4
+ * The deepProp argument is the name of property which should contain a nested array on which
5
+ * the function should also be applied recursively.
6
+ *
7
+ * @method map
8
+ * @global
9
+ * @example
10
+ * ```javascript
11
+ * const hours = bbn.fn.range(0, 23, 1);
12
+ * // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
13
+ * ```
14
+ * @example
15
+ * ```javascript
16
+ * const minsec = bbn.fn.range(0, 59, 1).map(h => ({text: h, value: h}));
17
+ * // [{text: 0, value: 0}, {text: 1, value: 1}, ..., {text: 59, value: 59}]
18
+ * ```
19
+ *
20
+ * @memberof bbn.fn
21
+ * @param {Number} start
22
+ * @param {Number} stop
23
+ * @param {Number} step
24
+ * @returns {Array}
25
+ */
26
+ export default function range(start: number, stop: number, step: number): number[];
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Returns a new array generated by the execution of a function for each item of the given array.
3
+ *
4
+ * The deepProp argument is the name of property which should contain a nested array on which
5
+ * the function should also be applied recursively.
6
+ *
7
+ * @method map
8
+ * @global
9
+ * @example
10
+ * ```javascript
11
+ * const hours = bbn.fn.range(0, 23, 1);
12
+ * // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
13
+ * ```
14
+ * @example
15
+ * ```javascript
16
+ * const minsec = bbn.fn.range(0, 59, 1).map(h => ({text: h, value: h}));
17
+ * // [{text: 0, value: 0}, {text: 1, value: 1}, ..., {text: 59, value: 59}]
18
+ * ```
19
+ *
20
+ * @memberof bbn.fn
21
+ * @param {Number} start
22
+ * @param {Number} stop
23
+ * @param {Number} step
24
+ * @returns {Array}
25
+ */
26
+ export default function range(start, stop, step) {
27
+ return Array.from({ length: (stop - start) / step + 1 }, (value, index) => start + index * step);
28
+ }
package/dist/fn.d.ts CHANGED
@@ -197,6 +197,7 @@ import printf from './fn/string/printf.js';
197
197
  import quotes2html from './fn/string/quotes2html.js';
198
198
  import randomInt from './fn/misc/randomInt.js';
199
199
  import randomString from './fn/string/randomString.js';
200
+ import range from './fn/object/range.js';
200
201
  import removeAccents from './fn/string/removeAccents.js';
201
202
  import removeEmpty from './fn/object/removeEmpty.js';
202
203
  import removeExtraSpaces from './fn/string/removeExtraSpaces.js';
@@ -444,6 +445,7 @@ declare const _default: {
444
445
  quotes2html: typeof quotes2html;
445
446
  randomInt: typeof randomInt;
446
447
  randomString: typeof randomString;
448
+ range: typeof range;
447
449
  removeAccents: typeof removeAccents;
448
450
  removeEmpty: typeof removeEmpty;
449
451
  removeExtraSpaces: typeof removeExtraSpaces;
package/dist/fn.js CHANGED
@@ -197,6 +197,7 @@ import printf from './fn/string/printf.js';
197
197
  import quotes2html from './fn/string/quotes2html.js';
198
198
  import randomInt from './fn/misc/randomInt.js';
199
199
  import randomString from './fn/string/randomString.js';
200
+ import range from './fn/object/range.js';
200
201
  import removeAccents from './fn/string/removeAccents.js';
201
202
  import removeEmpty from './fn/object/removeEmpty.js';
202
203
  import removeExtraSpaces from './fn/string/removeExtraSpaces.js';
@@ -444,6 +445,7 @@ export default {
444
445
  quotes2html,
445
446
  randomInt,
446
447
  randomString,
448
+ range,
447
449
  removeAccents,
448
450
  removeEmpty,
449
451
  removeExtraSpaces,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "2.0.21",
3
+ "version": "2.0.23",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",