@punks/backend-entity-manager 0.0.365 → 0.0.366

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.
@@ -24,6 +24,7 @@ export type EntitySerializerColumnValidator<TSheetItem> = {
24
24
  key: string;
25
25
  fn: (item: TSheetItem) => EntryValidationResult;
26
26
  };
27
+ export type EntitySerializerSheetCustomParser = "date";
27
28
  export type EntitySerializerColumnDefinition<TSheetItem> = {
28
29
  name: string;
29
30
  key: string;
@@ -35,6 +36,7 @@ export type EntitySerializerColumnDefinition<TSheetItem> = {
35
36
  converter?: EntitySerializerColumnConverter<TSheetItem>;
36
37
  validators?: EntitySerializerColumnValidator<TSheetItem>[];
37
38
  idColumn?: boolean;
39
+ sheetParser?: EntitySerializerSheetCustomParser;
38
40
  };
39
41
  export type EntitySerializerSheetDefinition<TSheetItem> = {
40
42
  columns: EntitySerializerColumnDefinition<TSheetItem>[];
package/dist/esm/index.js CHANGED
@@ -216,8 +216,10 @@ class EntitySerializer {
216
216
  return records.map((x, i) => this.convertSheetRecord(x, definition, i));
217
217
  }
218
218
  parseXlsx(data, definition) {
219
+ const dateColumns = definition.columns.filter((x) => x.sheetParser === "date");
219
220
  const records = excelParse(data, {
220
221
  keysTransform: ExcelKeyTransform.Lower,
222
+ dateColumns: dateColumns.map((x) => x.name),
221
223
  });
222
224
  return records.map((x, i) => this.convertSheetRecord(x, definition, i));
223
225
  }
@@ -22220,26 +22222,6 @@ MediaLibraryService = __decorate([
22220
22222
  __metadata("design:paramtypes", [EntityManagerRegistry])
22221
22223
  ], MediaLibraryService);
22222
22224
 
22223
- function toInteger(dirtyNumber) {
22224
- if (dirtyNumber === null || dirtyNumber === true || dirtyNumber === false) {
22225
- return NaN;
22226
- }
22227
-
22228
- var number = Number(dirtyNumber);
22229
-
22230
- if (isNaN(number)) {
22231
- return number;
22232
- }
22233
-
22234
- return number < 0 ? Math.ceil(number) : Math.floor(number);
22235
- }
22236
-
22237
- function requiredArgs(required, args) {
22238
- if (args.length < required) {
22239
- throw new TypeError(required + ' argument' + (required > 1 ? 's' : '') + ' required, but only ' + args.length + ' present');
22240
- }
22241
- }
22242
-
22243
22225
  /**
22244
22226
  * @name toDate
22245
22227
  * @category Common Helpers
@@ -22256,9 +22238,11 @@ function requiredArgs(required, args) {
22256
22238
  *
22257
22239
  * **Note**: *all* Date arguments passed to any *date-fns* function is processed by `toDate`.
22258
22240
  *
22259
- * @param {Date|Number} argument - the value to convert
22260
- * @returns {Date} the parsed date in the local time zone
22261
- * @throws {TypeError} 1 argument required
22241
+ * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
22242
+ *
22243
+ * @param argument - The value to convert
22244
+ *
22245
+ * @returns The parsed date in the local time zone
22262
22246
  *
22263
22247
  * @example
22264
22248
  * // Clone the date:
@@ -22270,28 +22254,68 @@ function requiredArgs(required, args) {
22270
22254
  * const result = toDate(1392098430000)
22271
22255
  * //=> Tue Feb 11 2014 11:30:30
22272
22256
  */
22273
-
22274
22257
  function toDate(argument) {
22275
- requiredArgs(1, arguments);
22276
- var argStr = Object.prototype.toString.call(argument); // Clone the date
22258
+ const argStr = Object.prototype.toString.call(argument);
22277
22259
 
22278
- if (argument instanceof Date || typeof argument === 'object' && argStr === '[object Date]') {
22260
+ // Clone the date
22261
+ if (
22262
+ argument instanceof Date ||
22263
+ (typeof argument === "object" && argStr === "[object Date]")
22264
+ ) {
22279
22265
  // Prevent the date to lose the milliseconds when passed to new Date() in IE10
22280
- return new Date(argument.getTime());
22281
- } else if (typeof argument === 'number' || argStr === '[object Number]') {
22266
+ return new argument.constructor(+argument);
22267
+ } else if (
22268
+ typeof argument === "number" ||
22269
+ argStr === "[object Number]" ||
22270
+ typeof argument === "string" ||
22271
+ argStr === "[object String]"
22272
+ ) {
22273
+ // TODO: Can we get rid of as?
22282
22274
  return new Date(argument);
22283
22275
  } else {
22284
- if ((typeof argument === 'string' || argStr === '[object String]') && typeof console !== 'undefined') {
22285
- // eslint-disable-next-line no-console
22286
- console.warn("Starting with v2.0.0-beta.1 date-fns doesn't accept strings as date arguments. Please use `parseISO` to parse strings. See: https://git.io/fjule"); // eslint-disable-next-line no-console
22287
-
22288
- console.warn(new Error().stack);
22289
- }
22290
-
22276
+ // TODO: Can we get rid of as?
22291
22277
  return new Date(NaN);
22292
22278
  }
22293
22279
  }
22294
22280
 
22281
+ /**
22282
+ * @name constructFrom
22283
+ * @category Generic Helpers
22284
+ * @summary Constructs a date using the reference date and the value
22285
+ *
22286
+ * @description
22287
+ * The function constructs a new date using the constructor from the reference
22288
+ * date and the given value. It helps to build generic functions that accept
22289
+ * date extensions.
22290
+ *
22291
+ * It defaults to `Date` if the passed reference date is a number or a string.
22292
+ *
22293
+ * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
22294
+ *
22295
+ * @param date - The reference date to take constructor from
22296
+ * @param value - The value to create the date
22297
+ *
22298
+ * @returns Date initialized using the given date and value
22299
+ *
22300
+ * @example
22301
+ * import { constructFrom } from 'date-fns'
22302
+ *
22303
+ * // A function that clones a date preserving the original type
22304
+ * function cloneDate<DateType extends Date(date: DateType): DateType {
22305
+ * return constructFrom(
22306
+ * date, // Use contrustor from the given date
22307
+ * date.getTime() // Use the date value to create a new date
22308
+ * )
22309
+ * }
22310
+ */
22311
+ function constructFrom(date, value) {
22312
+ if (date instanceof Date) {
22313
+ return new date.constructor(value);
22314
+ } else {
22315
+ return new Date(value);
22316
+ }
22317
+ }
22318
+
22295
22319
  /**
22296
22320
  * @name addDays
22297
22321
  * @category Day Helpers
@@ -22300,37 +22324,60 @@ function toDate(argument) {
22300
22324
  * @description
22301
22325
  * Add the specified number of days to the given date.
22302
22326
  *
22303
- * ### v2.0.0 breaking changes:
22327
+ * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
22304
22328
  *
22305
- * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
22329
+ * @param date - The date to be changed
22330
+ * @param amount - The amount of days to be added.
22306
22331
  *
22307
- * @param {Date|Number} date - the date to be changed
22308
- * @param {Number} amount - the amount of days to be added. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`.
22309
- * @returns {Date} the new date with the days added
22310
- * @throws {TypeError} 2 arguments required
22332
+ * @returns The new date with the days added
22311
22333
  *
22312
22334
  * @example
22313
22335
  * // Add 10 days to 1 September 2014:
22314
- * var result = addDays(new Date(2014, 8, 1), 10)
22336
+ * const result = addDays(new Date(2014, 8, 1), 10)
22315
22337
  * //=> Thu Sep 11 2014 00:00:00
22316
22338
  */
22317
-
22318
- function addDays(dirtyDate, dirtyAmount) {
22319
- requiredArgs(2, arguments);
22320
- var date = toDate(dirtyDate);
22321
- var amount = toInteger(dirtyAmount);
22322
-
22323
- if (isNaN(amount)) {
22324
- return new Date(NaN);
22325
- }
22326
-
22339
+ function addDays(date, amount) {
22340
+ const _date = toDate(date);
22341
+ if (isNaN(amount)) return constructFrom(date, NaN);
22327
22342
  if (!amount) {
22328
22343
  // If 0 days, no-op to avoid changing times in the hour before end of DST
22329
- return date;
22344
+ return _date;
22330
22345
  }
22346
+ _date.setDate(_date.getDate() + amount);
22347
+ return _date;
22348
+ }
22349
+
22350
+ /**
22351
+ * @module constants
22352
+ * @summary Useful constants
22353
+ * @description
22354
+ * Collection of useful date constants.
22355
+ *
22356
+ * The constants could be imported from `date-fns/constants`:
22357
+ *
22358
+ * ```ts
22359
+ * import { maxTime, minTime } from "./constants/date-fns/constants";
22360
+ *
22361
+ * function isAllowedTime(time) {
22362
+ * return time <= maxTime && time >= minTime;
22363
+ * }
22364
+ * ```
22365
+ */
22331
22366
 
22332
- date.setDate(date.getDate() + amount);
22333
- return date;
22367
+ /**
22368
+ * @constant
22369
+ * @name millisecondsInMinute
22370
+ * @summary Milliseconds in 1 minute
22371
+ */
22372
+ const millisecondsInMinute = 60000;
22373
+
22374
+ function getRoundingMethod(method) {
22375
+ return (number) => {
22376
+ const round = method ? Math[method] : Math.trunc;
22377
+ const result = round(number);
22378
+ // Prevent negative zero
22379
+ return result === 0 ? 0 : result;
22380
+ };
22334
22381
  }
22335
22382
 
22336
22383
  /**
@@ -22341,33 +22388,30 @@ function addDays(dirtyDate, dirtyAmount) {
22341
22388
  * @description
22342
22389
  * Get the number of milliseconds between the given dates.
22343
22390
  *
22344
- * ### v2.0.0 breaking changes:
22391
+ * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
22345
22392
  *
22346
- * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
22393
+ * @param dateLeft - The later date
22394
+ * @param dateRight - The earlier date
22347
22395
  *
22348
- * @param {Date|Number} dateLeft - the later date
22349
- * @param {Date|Number} dateRight - the earlier date
22350
- * @returns {Number} the number of milliseconds
22351
- * @throws {TypeError} 2 arguments required
22396
+ * @returns The number of milliseconds
22352
22397
  *
22353
22398
  * @example
22354
22399
  * // How many milliseconds are between
22355
22400
  * // 2 July 2014 12:30:20.600 and 2 July 2014 12:30:21.700?
22356
- * var result = differenceInMilliseconds(
22401
+ * const result = differenceInMilliseconds(
22357
22402
  * new Date(2014, 6, 2, 12, 30, 21, 700),
22358
22403
  * new Date(2014, 6, 2, 12, 30, 20, 600)
22359
22404
  * )
22360
22405
  * //=> 1100
22361
22406
  */
22362
-
22363
- function differenceInMilliseconds(dirtyDateLeft, dirtyDateRight) {
22364
- requiredArgs(2, arguments);
22365
- var dateLeft = toDate(dirtyDateLeft);
22366
- var dateRight = toDate(dirtyDateRight);
22367
- return dateLeft.getTime() - dateRight.getTime();
22407
+ function differenceInMilliseconds(dateLeft, dateRight) {
22408
+ return +toDate(dateLeft) - +toDate(dateRight);
22368
22409
  }
22369
22410
 
22370
- var MILLISECONDS_IN_MINUTE = 60000;
22411
+ /**
22412
+ * The {@link differenceInMinutes} function options.
22413
+ */
22414
+
22371
22415
  /**
22372
22416
  * @name differenceInMinutes
22373
22417
  * @category Minute Helpers
@@ -22376,36 +22420,34 @@ var MILLISECONDS_IN_MINUTE = 60000;
22376
22420
  * @description
22377
22421
  * Get the signed number of full (rounded towards 0) minutes between the given dates.
22378
22422
  *
22379
- * ### v2.0.0 breaking changes:
22423
+ * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
22380
22424
  *
22381
- * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
22425
+ * @param dateLeft - The later date
22426
+ * @param dateRight - The earlier date
22427
+ * @param options - An object with options.
22382
22428
  *
22383
- * @param {Date|Number} dateLeft - the later date
22384
- * @param {Date|Number} dateRight - the earlier date
22385
- * @returns {Number} the number of minutes
22386
- * @throws {TypeError} 2 arguments required
22429
+ * @returns The number of minutes
22387
22430
  *
22388
22431
  * @example
22389
22432
  * // How many minutes are between 2 July 2014 12:07:59 and 2 July 2014 12:20:00?
22390
- * var result = differenceInMinutes(
22433
+ * const result = differenceInMinutes(
22391
22434
  * new Date(2014, 6, 2, 12, 20, 0),
22392
22435
  * new Date(2014, 6, 2, 12, 7, 59)
22393
22436
  * )
22394
22437
  * //=> 12
22395
22438
  *
22396
22439
  * @example
22397
- * // How many minutes are from 10:01:59 to 10:00:00
22398
- * var result = differenceInMinutes(
22440
+ * // How many minutes are between 10:01:59 and 10:00:00
22441
+ * const result = differenceInMinutes(
22399
22442
  * new Date(2000, 0, 1, 10, 0, 0),
22400
22443
  * new Date(2000, 0, 1, 10, 1, 59)
22401
22444
  * )
22402
22445
  * //=> -1
22403
22446
  */
22404
-
22405
- function differenceInMinutes(dirtyDateLeft, dirtyDateRight) {
22406
- requiredArgs(2, arguments);
22407
- var diff = differenceInMilliseconds(dirtyDateLeft, dirtyDateRight) / MILLISECONDS_IN_MINUTE;
22408
- return diff > 0 ? Math.floor(diff) : Math.ceil(diff);
22447
+ function differenceInMinutes(dateLeft, dateRight, options) {
22448
+ const diff =
22449
+ differenceInMilliseconds(dateLeft, dateRight) / millisecondsInMinute;
22450
+ return getRoundingMethod(options?.roundingMethod)(diff);
22409
22451
  }
22410
22452
 
22411
22453
  /**
@@ -22416,25 +22458,20 @@ function differenceInMinutes(dirtyDateLeft, dirtyDateRight) {
22416
22458
  * @description
22417
22459
  * Subtract the specified number of days from the given date.
22418
22460
  *
22419
- * ### v2.0.0 breaking changes:
22461
+ * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
22420
22462
  *
22421
- * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
22463
+ * @param date - The date to be changed
22464
+ * @param amount - The amount of days to be subtracted.
22422
22465
  *
22423
- * @param {Date|Number} date - the date to be changed
22424
- * @param {Number} amount - the amount of days to be subtracted. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`.
22425
- * @returns {Date} the new date with the days subtracted
22426
- * @throws {TypeError} 2 arguments required
22466
+ * @returns The new date with the days subtracted
22427
22467
  *
22428
22468
  * @example
22429
22469
  * // Subtract 10 days from 1 September 2014:
22430
- * var result = subDays(new Date(2014, 8, 1), 10)
22470
+ * const result = subDays(new Date(2014, 8, 1), 10)
22431
22471
  * //=> Fri Aug 22 2014 00:00:00
22432
22472
  */
22433
-
22434
- function subDays(dirtyDate, dirtyAmount) {
22435
- requiredArgs(2, arguments);
22436
- var amount = toInteger(dirtyAmount);
22437
- return addDays(dirtyDate, -amount);
22473
+ function subDays(date, amount) {
22474
+ return addDays(date, -amount);
22438
22475
  }
22439
22476
 
22440
22477
  const WpOperationLockService = (props = {}) => applyDecorators(Injectable(), SetMetadata(EntityManagerSymbols.OperationLockService, {