@zauru-sdk/common 1.0.119 → 2.0.1

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/CHANGELOG.md CHANGED
@@ -3,6 +3,30 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [2.0.1](https://github.com/intuitiva/zauru-typescript-sdk/compare/v2.0.0...v2.0.1) (2024-09-27)
7
+
8
+ **Note:** Version bump only for package @zauru-sdk/common
9
+
10
+
11
+
12
+
13
+
14
+ # [2.0.0](https://github.com/intuitiva/zauru-typescript-sdk/compare/v1.0.120...v2.0.0) (2024-09-27)
15
+
16
+ **Note:** Version bump only for package @zauru-sdk/common
17
+
18
+
19
+
20
+
21
+
22
+ ## [1.0.120](https://github.com/intuitiva/zauru-typescript-sdk/compare/v1.0.119...v1.0.120) (2024-09-26)
23
+
24
+ **Note:** Version bump only for package @zauru-sdk/common
25
+
26
+
27
+
28
+
29
+
6
30
  ## [1.0.119](https://github.com/intuitiva/zauru-typescript-sdk/compare/v1.0.117...v1.0.119) (2024-09-26)
7
31
 
8
32
  **Note:** Version bump only for package @zauru-sdk/common
package/dist/common.d.ts CHANGED
@@ -163,3 +163,40 @@ export declare function calculateTimeDifference(timestamp: string): {
163
163
  minutes: number;
164
164
  label: string;
165
165
  };
166
+ /**
167
+ * Creates a unique code with a compact date format in uppercase.
168
+ *
169
+ * @param {string | null} suffix - An optional prefix to add to the code.
170
+ * @returns {string} A unique code consisting of a timestamp and an optional suffix.
171
+ *
172
+ * @example
173
+ * // Without suffix
174
+ * createCode(null); // Returns something like "1A2B3C4D"
175
+ *
176
+ * @example
177
+ * // With suffix
178
+ * createCode("INV"); // Returns something like "INV-1A2B3C4D"
179
+ */
180
+ export declare function createCode(suffix: string | null): string;
181
+ /**
182
+ * Parses a code generated by createCode function to extract its components.
183
+ *
184
+ * @param {string} code - The code to parse.
185
+ * @returns {Object} An object containing the parsed date and suffix (if any).
186
+ * @property {Date} date - The date represented by the timestamp in the code.
187
+ * @property {string | null} suffix - The suffix of the code, if present; otherwise null.
188
+ *
189
+ * @example
190
+ * // Parsing a code without suffix
191
+ * parseCode("1A2B3C4D");
192
+ * // Returns { date: Date object, suffix: null }
193
+ *
194
+ * @example
195
+ * // Parsing a code with suffix
196
+ * parseCode("INV-1A2B3C4D");
197
+ * // Returns { date: Date object, suffix: "INV" }
198
+ */
199
+ export declare function parseCode(code: string): {
200
+ date: Date;
201
+ suffix: string | null;
202
+ };
@@ -20,6 +20,8 @@ exports.capitalLetter = capitalLetter;
20
20
  exports.handlePossibleAxiosErrors = handlePossibleAxiosErrors;
21
21
  exports.createPostgresUrl = createPostgresUrl;
22
22
  exports.calculateTimeDifference = calculateTimeDifference;
23
+ exports.createCode = createCode;
24
+ exports.parseCode = parseCode;
23
25
  const moment_1 = __importDefault(require("moment"));
24
26
  require("moment-timezone");
25
27
  const types_1 = require("@zauru-sdk/types");
@@ -601,3 +603,53 @@ function calculateTimeDifference(timestamp) {
601
603
  label: `${hours > 0 ? `${hours} ${hours > 1 ? "horas" : "hora"}` : ""}${hours > 0 && minutes > 0 ? " y " : ""}${minutes > 0 ? `${minutes} ${minutes > 1 ? "minutos" : "minuto"}` : ""}.`,
602
604
  };
603
605
  }
606
+ /**
607
+ * Creates a unique code with a compact date format in uppercase.
608
+ *
609
+ * @param {string | null} suffix - An optional prefix to add to the code.
610
+ * @returns {string} A unique code consisting of a timestamp and an optional suffix.
611
+ *
612
+ * @example
613
+ * // Without suffix
614
+ * createCode(null); // Returns something like "1A2B3C4D"
615
+ *
616
+ * @example
617
+ * // With suffix
618
+ * createCode("INV"); // Returns something like "INV-1A2B3C4D"
619
+ */
620
+ function createCode(suffix) {
621
+ const timestamp = Math.floor(Date.now() / 1000)
622
+ .toString(36)
623
+ .toUpperCase(); // Timestamp in base 36 and converted to uppercase
624
+ const code = `${timestamp}`;
625
+ return suffix ? `${suffix}-${code}` : code;
626
+ }
627
+ /**
628
+ * Parses a code generated by createCode function to extract its components.
629
+ *
630
+ * @param {string} code - The code to parse.
631
+ * @returns {Object} An object containing the parsed date and suffix (if any).
632
+ * @property {Date} date - The date represented by the timestamp in the code.
633
+ * @property {string | null} suffix - The suffix of the code, if present; otherwise null.
634
+ *
635
+ * @example
636
+ * // Parsing a code without suffix
637
+ * parseCode("1A2B3C4D");
638
+ * // Returns { date: Date object, suffix: null }
639
+ *
640
+ * @example
641
+ * // Parsing a code with suffix
642
+ * parseCode("INV-1A2B3C4D");
643
+ * // Returns { date: Date object, suffix: "INV" }
644
+ */
645
+ function parseCode(code) {
646
+ // Check if the code has a suffix
647
+ const hasSuffix = code.includes("-") && isNaN(Number(code.split("-")[0]));
648
+ const [suffixOrTimestamp, timestamp] = code.split("-");
649
+ const dateTimestamp = hasSuffix ? timestamp : suffixOrTimestamp;
650
+ const date = new Date(parseInt(dateTimestamp, 36) * 1000); // Convert the timestamp back to milliseconds
651
+ return {
652
+ date: date,
653
+ suffix: hasSuffix ? suffixOrTimestamp : null, // Only return the suffix if it exists
654
+ };
655
+ }
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@zauru-sdk/common",
3
- "version": "1.0.119",
3
+ "version": "2.0.1",
4
4
  "description": "Funciones de uso común en el desarrollo de webapps en Zauru",
5
- "main": "./dist/cjs/index.js",
5
+ "main": "./dist/esm/index.js",
6
6
  "module": "./dist/esm/index.js",
7
7
  "types": "./dist/index.d.ts",
8
8
  "publishConfig": {
@@ -20,9 +20,9 @@
20
20
  },
21
21
  "dependencies": {
22
22
  "@remix-run/node": "^2.8.1",
23
- "@zauru-sdk/types": "^1.0.119",
23
+ "@zauru-sdk/types": "^2.0.0",
24
24
  "moment": "^2.30.1",
25
25
  "moment-timezone": "^0.5.45"
26
26
  },
27
- "gitHead": "61a0842f7e502c72e54defddb197fa69aa58f422"
27
+ "gitHead": "fb18e2974e5ec5a211f60966948ece0e8d902547"
28
28
  }
package/src/common.ts CHANGED
@@ -684,3 +684,58 @@ export function calculateTimeDifference(timestamp: string): {
684
684
  }${minutes > 0 ? `${minutes} ${minutes > 1 ? "minutos" : "minuto"}` : ""}.`,
685
685
  };
686
686
  }
687
+
688
+ /**
689
+ * Creates a unique code with a compact date format in uppercase.
690
+ *
691
+ * @param {string | null} suffix - An optional prefix to add to the code.
692
+ * @returns {string} A unique code consisting of a timestamp and an optional suffix.
693
+ *
694
+ * @example
695
+ * // Without suffix
696
+ * createCode(null); // Returns something like "1A2B3C4D"
697
+ *
698
+ * @example
699
+ * // With suffix
700
+ * createCode("INV"); // Returns something like "INV-1A2B3C4D"
701
+ */
702
+ export function createCode(suffix: string | null): string {
703
+ const timestamp = Math.floor(Date.now() / 1000)
704
+ .toString(36)
705
+ .toUpperCase(); // Timestamp in base 36 and converted to uppercase
706
+ const code = `${timestamp}`;
707
+
708
+ return suffix ? `${suffix}-${code}` : code;
709
+ }
710
+
711
+ /**
712
+ * Parses a code generated by createCode function to extract its components.
713
+ *
714
+ * @param {string} code - The code to parse.
715
+ * @returns {Object} An object containing the parsed date and suffix (if any).
716
+ * @property {Date} date - The date represented by the timestamp in the code.
717
+ * @property {string | null} suffix - The suffix of the code, if present; otherwise null.
718
+ *
719
+ * @example
720
+ * // Parsing a code without suffix
721
+ * parseCode("1A2B3C4D");
722
+ * // Returns { date: Date object, suffix: null }
723
+ *
724
+ * @example
725
+ * // Parsing a code with suffix
726
+ * parseCode("INV-1A2B3C4D");
727
+ * // Returns { date: Date object, suffix: "INV" }
728
+ */
729
+ export function parseCode(code: string) {
730
+ // Check if the code has a suffix
731
+ const hasSuffix = code.includes("-") && isNaN(Number(code.split("-")[0]));
732
+ const [suffixOrTimestamp, timestamp] = code.split("-");
733
+
734
+ const dateTimestamp = hasSuffix ? timestamp : suffixOrTimestamp;
735
+ const date = new Date(parseInt(dateTimestamp, 36) * 1000); // Convert the timestamp back to milliseconds
736
+
737
+ return {
738
+ date: date,
739
+ suffix: hasSuffix ? suffixOrTimestamp : null, // Only return the suffix if it exists
740
+ };
741
+ }