@kwiz/common 1.0.78 → 1.0.80

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.
Files changed (118) hide show
  1. package/.github/workflows/npm-publish.yml +24 -0
  2. package/.madgerc +2 -2
  3. package/LICENSE +21 -21
  4. package/fix-folder-imports.js +26 -26
  5. package/lib/cjs/helpers/sharepoint.js +5 -1
  6. package/lib/cjs/helpers/sharepoint.js.map +1 -1
  7. package/lib/cjs/helpers/typecheckers.js +5 -1
  8. package/lib/cjs/helpers/typecheckers.js.map +1 -1
  9. package/lib/cjs/types/libs/msal.types.js +26 -26
  10. package/lib/cjs/utils/sharepoint.rest/list.js +1 -1
  11. package/lib/cjs/utils/sharepoint.rest/list.js.map +1 -1
  12. package/lib/cjs/utils/sharepoint.rest/user.js +11 -11
  13. package/lib/esm/helpers/sharepoint.js +3 -0
  14. package/lib/esm/helpers/sharepoint.js.map +1 -1
  15. package/lib/esm/helpers/typecheckers.js +3 -0
  16. package/lib/esm/helpers/typecheckers.js.map +1 -1
  17. package/lib/esm/types/libs/msal.types.js +26 -26
  18. package/lib/esm/utils/sharepoint.rest/list.js +2 -2
  19. package/lib/esm/utils/sharepoint.rest/list.js.map +1 -1
  20. package/lib/esm/utils/sharepoint.rest/user.js +11 -11
  21. package/lib/types/helpers/sharepoint.d.ts +1 -0
  22. package/lib/types/helpers/typecheckers.d.ts +1 -0
  23. package/package.json +77 -77
  24. package/readme.md +17 -17
  25. package/src/_dependencies.ts +12 -12
  26. package/src/config.ts +17 -17
  27. package/src/helpers/Guid.ts +181 -181
  28. package/src/helpers/base64.ts +173 -173
  29. package/src/helpers/browser.test.js +13 -13
  30. package/src/helpers/browser.ts +1348 -1348
  31. package/src/helpers/browserinfo.ts +292 -292
  32. package/src/helpers/collections.base.test.js +25 -25
  33. package/src/helpers/collections.base.ts +437 -437
  34. package/src/helpers/collections.ts +107 -107
  35. package/src/helpers/color.ts +54 -54
  36. package/src/helpers/cookies.ts +59 -59
  37. package/src/helpers/date.test.js +119 -119
  38. package/src/helpers/date.ts +188 -188
  39. package/src/helpers/debug.ts +186 -186
  40. package/src/helpers/emails.ts +6 -6
  41. package/src/helpers/eval.ts +5 -5
  42. package/src/helpers/file.test.js +50 -50
  43. package/src/helpers/file.ts +58 -58
  44. package/src/helpers/flatted.ts +149 -149
  45. package/src/helpers/functions.ts +16 -16
  46. package/src/helpers/graph/calendar.types.ts +10 -10
  47. package/src/helpers/http.ts +69 -69
  48. package/src/helpers/images.ts +22 -22
  49. package/src/helpers/json.ts +38 -38
  50. package/src/helpers/md5.ts +189 -189
  51. package/src/helpers/objects.test.js +33 -33
  52. package/src/helpers/objects.ts +270 -270
  53. package/src/helpers/promises.test.js +37 -37
  54. package/src/helpers/promises.ts +165 -165
  55. package/src/helpers/random.ts +27 -27
  56. package/src/helpers/scheduler/scheduler.test.js +103 -103
  57. package/src/helpers/scheduler/scheduler.ts +131 -131
  58. package/src/helpers/sharepoint.ts +776 -772
  59. package/src/helpers/strings.test.js +101 -101
  60. package/src/helpers/strings.ts +317 -317
  61. package/src/helpers/typecheckers.test.js +34 -34
  62. package/src/helpers/typecheckers.ts +266 -262
  63. package/src/helpers/url.test.js +43 -43
  64. package/src/helpers/url.ts +207 -207
  65. package/src/helpers/urlhelper.ts +111 -111
  66. package/src/index.ts +6 -6
  67. package/src/types/auth.ts +54 -54
  68. package/src/types/common.types.ts +15 -15
  69. package/src/types/flatted.types.ts +59 -59
  70. package/src/types/globals.types.ts +6 -6
  71. package/src/types/graph/calendar.types.ts +80 -80
  72. package/src/types/knownscript.types.ts +18 -18
  73. package/src/types/libs/datajs.types.ts +28 -28
  74. package/src/types/libs/ics.types.ts +30 -30
  75. package/src/types/libs/msal.types.ts +49 -49
  76. package/src/types/locales.ts +124 -124
  77. package/src/types/localstoragecache.types.ts +8 -8
  78. package/src/types/location.types.ts +27 -27
  79. package/src/types/moment.ts +11 -11
  80. package/src/types/regex.types.ts +16 -16
  81. package/src/types/rest.types.ts +95 -95
  82. package/src/types/sharepoint.types.ts +1465 -1465
  83. package/src/types/sharepoint.utils.types.ts +287 -287
  84. package/src/utils/auth/common.ts +74 -74
  85. package/src/utils/auth/discovery.test.js +12 -12
  86. package/src/utils/auth/discovery.ts +132 -132
  87. package/src/utils/base64.ts +27 -27
  88. package/src/utils/consolelogger.ts +320 -320
  89. package/src/utils/date.ts +35 -35
  90. package/src/utils/emails.ts +24 -24
  91. package/src/utils/knownscript.ts +286 -286
  92. package/src/utils/localstoragecache.ts +441 -441
  93. package/src/utils/rest.ts +501 -501
  94. package/src/utils/script.ts +170 -170
  95. package/src/utils/sharepoint.rest/common.ts +154 -154
  96. package/src/utils/sharepoint.rest/date.ts +62 -62
  97. package/src/utils/sharepoint.rest/file.folder.ts +598 -598
  98. package/src/utils/sharepoint.rest/item.ts +547 -547
  99. package/src/utils/sharepoint.rest/list.ts +1388 -1388
  100. package/src/utils/sharepoint.rest/listutils/GetListItemsByCaml.ts +774 -774
  101. package/src/utils/sharepoint.rest/listutils/GetListItemsById.ts +275 -275
  102. package/src/utils/sharepoint.rest/listutils/common.ts +206 -206
  103. package/src/utils/sharepoint.rest/location.ts +141 -141
  104. package/src/utils/sharepoint.rest/navigation-links.ts +86 -86
  105. package/src/utils/sharepoint.rest/user-search.ts +252 -252
  106. package/src/utils/sharepoint.rest/user.ts +491 -491
  107. package/src/utils/sharepoint.rest/web.ts +1384 -1384
  108. package/src/utils/sod.ts +194 -194
  109. package/lib/cjs/helpers/_dependencies.js +0 -21
  110. package/lib/cjs/helpers/_dependencies.js.map +0 -1
  111. package/lib/cjs/utils/_dependencies.js +0 -24
  112. package/lib/cjs/utils/_dependencies.js.map +0 -1
  113. package/lib/esm/helpers/_dependencies.js +0 -3
  114. package/lib/esm/helpers/_dependencies.js.map +0 -1
  115. package/lib/esm/utils/_dependencies.js +0 -4
  116. package/lib/esm/utils/_dependencies.js.map +0 -1
  117. package/lib/types/helpers/_dependencies.d.ts +0 -2
  118. package/lib/types/utils/_dependencies.d.ts +0 -3
package/package.json CHANGED
@@ -1,77 +1,77 @@
1
- {
2
- "name": "@kwiz/common",
3
- "version": "1.0.78",
4
- "description": "KWIZ common utilities and helpers for M365 platform",
5
- "main": "lib/cjs/index.js",
6
- "module": "lib/esm/index.js",
7
- "types": "lib/types/index.d.ts",
8
- "sideEffects": [
9
- "*.scss*",
10
- "lib/esm/helpers/debug.js",
11
- "lib/esm/config.js"
12
- ],
13
- "exports": {
14
- ".": {
15
- "types": "./lib/types/index.d.ts",
16
- "import": "./lib/esm/index.js",
17
- "require": "./lib/cjs/index.js"
18
- },
19
- "./package.json": "./package.json"
20
- },
21
- "scripts": {
22
- "watch": "tsc -watch",
23
- "build": "npm run reindex-project && npm run test && npm run build:esm && npm run build:cjs",
24
- "build:esm": "tsc -p tsconfig.esm.json",
25
- "build:cjs": "tsc -p tsconfig.cjs.json",
26
- "build-explain": "tsc --explainFiles",
27
- "check-dependencies": "madge --circular ./src",
28
- "create-link": "npm link",
29
- "test": "node --import tsx --test src",
30
- "npm-v-patch": "npm version patch",
31
- "npm-v-major": "npm version major",
32
- "npm-publish": "npm publish --access public",
33
- "reset-repo": "git fetch origin && git reset --hard origin/main",
34
- "reindex-project": "cti create ./src -i _dependencies -w -b -n -o exports-index.ts && node fix-folder-imports.js"
35
- },
36
- "repository": {
37
- "type": "git",
38
- "url": "git+https://github.com/KWizCom/common.git"
39
- },
40
- "keywords": [
41
- "KWIZ",
42
- "SharePoint",
43
- "SPO",
44
- "Teams",
45
- "Utilities",
46
- "Helpers"
47
- ],
48
- "author": "Shai Petel",
49
- "contributors": [
50
- "Shai Petel",
51
- "Kevin Vieira"
52
- ],
53
- "license": "MIT",
54
- "bugs": {
55
- "url": "https://github.com/KWizCom/common/issues",
56
- "email": "support@kwizcom.com"
57
- },
58
- "homepage": "https://github.com/KWizCom/common#readme",
59
- "private": false,
60
- "engines": {
61
- "node": ">=16"
62
- },
63
- "packageManager": "npm@9.5.1",
64
- "devDependencies": {
65
- "create-ts-index": "^1.14.0",
66
- "fs-extra": "^11.2.0",
67
- "madge": "^6.1.0",
68
- "tsx": "^4.7.1",
69
- "typescript": "^5.3.3",
70
- "xhr2": "^0.2.1"
71
- },
72
- "dependencies": {
73
- "esbuild": "^0.19.12",
74
- "get-tsconfig": "^4.7.2",
75
- "resolve-pkg-maps": "^1.0.0"
76
- }
77
- }
1
+ {
2
+ "name": "@kwiz/common",
3
+ "version": "1.0.80",
4
+ "description": "KWIZ common utilities and helpers for M365 platform",
5
+ "main": "lib/cjs/index.js",
6
+ "module": "lib/esm/index.js",
7
+ "types": "lib/types/index.d.ts",
8
+ "sideEffects": [
9
+ "*.scss*",
10
+ "lib/esm/helpers/debug.js",
11
+ "lib/esm/config.js"
12
+ ],
13
+ "exports": {
14
+ ".": {
15
+ "types": "./lib/types/index.d.ts",
16
+ "import": "./lib/esm/index.js",
17
+ "require": "./lib/cjs/index.js"
18
+ },
19
+ "./package.json": "./package.json"
20
+ },
21
+ "scripts": {
22
+ "watch": "tsc -watch",
23
+ "build": "npm run reindex-project && npm run test && npm run build:esm && npm run build:cjs",
24
+ "build:esm": "tsc -p tsconfig.esm.json",
25
+ "build:cjs": "tsc -p tsconfig.cjs.json",
26
+ "build-explain": "tsc --explainFiles",
27
+ "check-dependencies": "madge --circular ./src",
28
+ "create-link": "npm link",
29
+ "test": "node --import tsx --test src",
30
+ "npm-v-patch": "npm version patch && git push origin main:main && git push --tags",
31
+ "npm-v-major": "npm version major && git push origin main:main && git push --tags",
32
+ "npm-publish": "npm publish --access public",
33
+ "reset-repo": "git fetch origin && git reset --hard origin/main",
34
+ "reindex-project": "cti create ./src -i _dependencies -w -b -n -o exports-index.ts && node fix-folder-imports.js"
35
+ },
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "git+https://github.com/KWizCom/common.git"
39
+ },
40
+ "keywords": [
41
+ "KWIZ",
42
+ "SharePoint",
43
+ "SPO",
44
+ "Teams",
45
+ "Utilities",
46
+ "Helpers"
47
+ ],
48
+ "author": "Shai Petel",
49
+ "contributors": [
50
+ "Shai Petel",
51
+ "Kevin Vieira"
52
+ ],
53
+ "license": "MIT",
54
+ "bugs": {
55
+ "url": "https://github.com/KWizCom/common/issues",
56
+ "email": "support@kwizcom.com"
57
+ },
58
+ "homepage": "https://github.com/KWizCom/common#readme",
59
+ "private": false,
60
+ "engines": {
61
+ "node": ">=16"
62
+ },
63
+ "packageManager": "npm@9.5.1",
64
+ "devDependencies": {
65
+ "create-ts-index": "^1.14.0",
66
+ "fs-extra": "^11.2.0",
67
+ "madge": "^6.1.0",
68
+ "tsx": "^4.7.1",
69
+ "typescript": "^5.3.3",
70
+ "xhr2": "^0.2.1"
71
+ },
72
+ "dependencies": {
73
+ "esbuild": "^0.19.12",
74
+ "get-tsconfig": "^4.7.2",
75
+ "resolve-pkg-maps": "^1.0.0"
76
+ }
77
+ }
package/readme.md CHANGED
@@ -1,17 +1,17 @@
1
- # KWIZ Common Repo
2
-
3
- A collection of common helpers and utilities used across KWIZ projects
4
-
5
- To get started, configure our modules to your project by calling config:
6
-
7
- ```
8
- import { config } from "@kwiz/common";
9
- export const { logger: GetLogger } = config({
10
- BuildNumber: BuildNumber,
11
- //send true to have verbose logs and turn on debug mode
12
- IsLocalDev: IsLocalDev,
13
- ReleaseStatus: ReleaseStatus,
14
- //prefix logger with your project name
15
- ProjectName: "[cms]"
16
- });
17
- ```
1
+ # KWIZ Common Repo
2
+
3
+ A collection of common helpers and utilities used across KWIZ projects
4
+
5
+ To get started, configure our modules to your project by calling config:
6
+
7
+ ```
8
+ import { config } from "@kwiz/common";
9
+ export const { logger: GetLogger } = config({
10
+ BuildNumber: BuildNumber,
11
+ //send true to have verbose logs and turn on debug mode
12
+ IsLocalDev: IsLocalDev,
13
+ ReleaseStatus: ReleaseStatus,
14
+ //prefix logger with your project name
15
+ ProjectName: "[cms]"
16
+ });
17
+ ```
@@ -1,13 +1,13 @@
1
-
2
- export var BuildNumber = "unset";
3
- export var IsLocalDev = false;
4
- export var ReleaseStatus = "npm";
5
- export function SetDependencies(params: {
6
- BuildNumber?: string;
7
- IsLocalDev?: boolean;
8
- ReleaseStatus?: string;
9
- }) {
10
- if (typeof params.BuildNumber === "string") BuildNumber = params.BuildNumber;
11
- if (typeof params.IsLocalDev === "boolean") IsLocalDev = params.IsLocalDev;
12
- if (typeof params.ReleaseStatus === "string") ReleaseStatus = params.ReleaseStatus;
1
+
2
+ export var BuildNumber = "unset";
3
+ export var IsLocalDev = false;
4
+ export var ReleaseStatus = "npm";
5
+ export function SetDependencies(params: {
6
+ BuildNumber?: string;
7
+ IsLocalDev?: boolean;
8
+ ReleaseStatus?: string;
9
+ }) {
10
+ if (typeof params.BuildNumber === "string") BuildNumber = params.BuildNumber;
11
+ if (typeof params.IsLocalDev === "boolean") IsLocalDev = params.IsLocalDev;
12
+ if (typeof params.ReleaseStatus === "string") ReleaseStatus = params.ReleaseStatus;
13
13
  }
package/src/config.ts CHANGED
@@ -1,17 +1,17 @@
1
- import { SetDependencies } from './_dependencies';
2
- import { ConsoleLogger } from './utils/consolelogger';
3
-
4
- export function config(params: {
5
- BuildNumber?: string;
6
- IsLocalDev?: boolean;
7
- ReleaseStatus?: string;
8
- ProjectName?: string;
9
- }) {
10
- SetDependencies(params);
11
- function GetLogger(name: string) {
12
- return ConsoleLogger.get(name, params.ProjectName);
13
- }
14
- return {
15
- logger: GetLogger
16
- }
17
- }
1
+ import { SetDependencies } from './_dependencies';
2
+ import { ConsoleLogger } from './utils/consolelogger';
3
+
4
+ export function config(params: {
5
+ BuildNumber?: string;
6
+ IsLocalDev?: boolean;
7
+ ReleaseStatus?: string;
8
+ ProjectName?: string;
9
+ }) {
10
+ SetDependencies(params);
11
+ function GetLogger(name: string) {
12
+ return ConsoleLogger.get(name, params.ProjectName);
13
+ }
14
+ return {
15
+ logger: GetLogger
16
+ }
17
+ }
@@ -1,182 +1,182 @@
1
- /**
2
- * This class represents a globally unique identifier, as described by
3
- * IETF RFC 4122.
4
- *
5
- * @remarks
6
- * The input string is normalized and validated, which provides
7
- * important guarantees that simplify other code that works with the GUID.
8
- * This class also provides basic support for generating a pseudo-random GUID;
9
- * however, be aware that the uniqueness depends on the browser's `Math.random()`
10
- * function and may be not be suitable for some applications.
11
- *
12
- * See {@link https://www.ietf.org/rfc/rfc4122.txt | RFC4122} for more information.
13
- *
14
- * @public
15
- */
16
-
17
- import { isNullOrEmptyString, isNullOrUndefined } from "./typecheckers";
18
-
19
- export class Guid {
20
- private _guid;
21
- /**
22
- * Returns a new empty Guid instance.
23
- *
24
- * @returns A new empty Guid object.
25
- */
26
- public static empty = new Guid('00000000-0000-0000-0000-000000000000');
27
- private static _guidRegEx = /^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$/i;
28
-
29
- /**
30
- * Use `Guid.parse()` or `Guid.tryParse()` instead of the constructor.
31
- * @param guid - a normalized, already valid Guid string
32
- */
33
- constructor(guid) {
34
- this._guid = guid;
35
- }
36
- /**
37
- * Returns a new Guid instance with a pseudo-randomly generated GUID, according
38
- * to the version 4 UUID algorithm from RFC 4122.
39
- *
40
- * @returns A new unique Guid object
41
- */
42
- public static newGuid(randomNumberGenerator) {
43
- return new Guid(Guid._generateGuid(randomNumberGenerator));
44
- }
45
- /**
46
- * Parses the input string to construct a new Guid object.
47
- * If the string cannot be parsed, then an error is thrown.
48
- *
49
- * @remarks
50
- * Example syntaxes accepted by this function:
51
- *
52
- * - `"d5369f3bbd7a412a9c0f7f0650bb5489"`
53
- *
54
- * - `"d5369f3b-bd7a-412a-9c0f-7f0650bb5489"`
55
- *
56
- * - `"{d5369f3b-bd7a-412a-9c0f-7f0650bb5489}"`
57
- *
58
- * - `"/Guid(d5369f3b-bd7a-412a-9c0f-7f0650bb5489)/"`
59
- *
60
- * @param guid - The input string.
61
- * @returns A valid Guid object
62
- */
63
- public static parse(guidString) {
64
- var guid = Guid.tryParse(guidString);
65
- if (!guid) {
66
- throw new Error(`***Invalid GUID string: "${guidString}"`)
67
- }
68
- return guid;
69
- }
70
- /**
71
- * Attempts to parse the input string to construct a new Guid object.
72
- * If the string cannot be parsed, then undefined is returned.
73
- *
74
- * @remarks
75
- * Example syntaxes accepted by this function:
76
- *
77
- * - `"d5369f3bbd7a412a9c0f7f0650bb5489"`
78
- *
79
- * - `"d5369f3b-bd7a-412a-9c0f-7f0650bb5489"`
80
- *
81
- * - `"{d5369f3b-bd7a-412a-9c0f-7f0650bb5489}"`
82
- *
83
- * - `"/Guid(d5369f3b-bd7a-412a-9c0f-7f0650bb5489)/"`
84
- *
85
- * @param guid - The input string.
86
- * @returns The Guid object, or undefined if the string could not be parsed.
87
- */
88
- public static tryParse(guid) {
89
- if (guid) {
90
- guid = Guid._normalize(guid);
91
- if (Guid._guidRegEx.test(guid)) {
92
- return new Guid(guid);
93
- }
94
- }
95
- return undefined;
96
- }
97
- /**
98
- * Indicates whether a GUID is valid, i.e. whether it would be successfully
99
- * parsed by `Guid.tryParse()`. This function is cheaper than `Guid.tryParse()`
100
- * because it does not construct a Guid object.
101
- *
102
- * @param guid - The input string.
103
- * @returns true, if the Guid is valid.
104
- */
105
- public static isValid(guid) {
106
- if (guid) {
107
- guid = Guid._normalize(guid);
108
- if (Guid._guidRegEx.test(guid)) {
109
- return true;
110
- }
111
- }
112
- return false;
113
- }
114
- /**
115
- * SharePoint can have guids in the form:
116
- * - `Guid(...)`
117
- * - `{...}`
118
- * _normalize transforms guids in this format to the standard
119
- * guid format.
120
- *
121
- * Example: `Guid(d5369f3b-bd7a-412a-9c0f-7f0650bb5489)` becomes `d5369f3b-bd7a-412a-9c0f-7f0650bb5489`
122
- * Example: `{d5369f3b-bd7a-412a-9c0f-7f0650bb5489}` becomes `d5369f3b-bd7a-412a-9c0f-7f0650bb5489`
123
- *
124
- * @param guid - Guid to be normalized, could already be normalized.
125
- * @returns Normalized guid.
126
- */
127
- private static _normalize(guid) {
128
- if (isNullOrEmptyString(guid)) {
129
- throw new Error(`***The value for "guid" must not be an empty string`)
130
- }
131
- // Check for Guid(...) and strip
132
- var result = /^Guid\((.*)\)/i.exec(guid); /* tslint:disable-line:no-null-keyword */
133
- if (result) {
134
- guid = result[1];
135
- }
136
- // Check for and strip leading or trailing curly braces
137
- if (guid.length === 38) {
138
- guid = guid.substr(1, 36);
139
- }
140
- return guid.toLowerCase();
141
- }
142
- /**
143
- * Creates a new guid.
144
- *
145
- * @returns A valid guid (according to RFC4122)
146
- */
147
- private static _generateGuid(randomNumberGenerator) {
148
- return 'AAAAAAAA-AAAA-4AAA-BAAA-AAAAAAAAAAAA'.replace(/[AB]/g,
149
- // Callback for String.replace() when generating a guid.
150
- function (character) {
151
- var randomNumber = randomNumberGenerator ? randomNumberGenerator.generate() : Math.random();
152
- /* tslint:disable:no-bitwise */
153
- var num = randomNumber * 16 | 0;
154
- // Check for 'A' in template string because the first characters in the
155
- // third and fourth blocks must be specific characters (according to "version 4" UUID from RFC 4122)
156
- var masked = character === 'A' ? num : (num & 0x3 | 0x8);
157
- return masked.toString(16);
158
- });
159
- }
160
- /**
161
- * Compare this instance to another Guid instance
162
- *
163
- * @returns True if this instance and the specified Guid object
164
- * represent the same value.
165
- */
166
- public equals(guid) {
167
- if (isNullOrUndefined(guid)) {
168
- throw new Error(`"***The value for "guid" must not be null or undefined"`)
169
- }
170
- return guid && this._guid.toString() === guid.toString();
171
- }
172
- /**
173
- * Returns a string representation of the GUID
174
- *
175
- * @returns The GUID value in lowercase hexadecimal without braces.
176
- *
177
- * Example: `'d5369f3b-bd7a-412a-9c0f-7f0650bb5489'`
178
- */
179
- public toString = function () {
180
- return this._guid;
181
- }
1
+ /**
2
+ * This class represents a globally unique identifier, as described by
3
+ * IETF RFC 4122.
4
+ *
5
+ * @remarks
6
+ * The input string is normalized and validated, which provides
7
+ * important guarantees that simplify other code that works with the GUID.
8
+ * This class also provides basic support for generating a pseudo-random GUID;
9
+ * however, be aware that the uniqueness depends on the browser's `Math.random()`
10
+ * function and may be not be suitable for some applications.
11
+ *
12
+ * See {@link https://www.ietf.org/rfc/rfc4122.txt | RFC4122} for more information.
13
+ *
14
+ * @public
15
+ */
16
+
17
+ import { isNullOrEmptyString, isNullOrUndefined } from "./typecheckers";
18
+
19
+ export class Guid {
20
+ private _guid;
21
+ /**
22
+ * Returns a new empty Guid instance.
23
+ *
24
+ * @returns A new empty Guid object.
25
+ */
26
+ public static empty = new Guid('00000000-0000-0000-0000-000000000000');
27
+ private static _guidRegEx = /^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$/i;
28
+
29
+ /**
30
+ * Use `Guid.parse()` or `Guid.tryParse()` instead of the constructor.
31
+ * @param guid - a normalized, already valid Guid string
32
+ */
33
+ constructor(guid) {
34
+ this._guid = guid;
35
+ }
36
+ /**
37
+ * Returns a new Guid instance with a pseudo-randomly generated GUID, according
38
+ * to the version 4 UUID algorithm from RFC 4122.
39
+ *
40
+ * @returns A new unique Guid object
41
+ */
42
+ public static newGuid(randomNumberGenerator) {
43
+ return new Guid(Guid._generateGuid(randomNumberGenerator));
44
+ }
45
+ /**
46
+ * Parses the input string to construct a new Guid object.
47
+ * If the string cannot be parsed, then an error is thrown.
48
+ *
49
+ * @remarks
50
+ * Example syntaxes accepted by this function:
51
+ *
52
+ * - `"d5369f3bbd7a412a9c0f7f0650bb5489"`
53
+ *
54
+ * - `"d5369f3b-bd7a-412a-9c0f-7f0650bb5489"`
55
+ *
56
+ * - `"{d5369f3b-bd7a-412a-9c0f-7f0650bb5489}"`
57
+ *
58
+ * - `"/Guid(d5369f3b-bd7a-412a-9c0f-7f0650bb5489)/"`
59
+ *
60
+ * @param guid - The input string.
61
+ * @returns A valid Guid object
62
+ */
63
+ public static parse(guidString) {
64
+ var guid = Guid.tryParse(guidString);
65
+ if (!guid) {
66
+ throw new Error(`***Invalid GUID string: "${guidString}"`)
67
+ }
68
+ return guid;
69
+ }
70
+ /**
71
+ * Attempts to parse the input string to construct a new Guid object.
72
+ * If the string cannot be parsed, then undefined is returned.
73
+ *
74
+ * @remarks
75
+ * Example syntaxes accepted by this function:
76
+ *
77
+ * - `"d5369f3bbd7a412a9c0f7f0650bb5489"`
78
+ *
79
+ * - `"d5369f3b-bd7a-412a-9c0f-7f0650bb5489"`
80
+ *
81
+ * - `"{d5369f3b-bd7a-412a-9c0f-7f0650bb5489}"`
82
+ *
83
+ * - `"/Guid(d5369f3b-bd7a-412a-9c0f-7f0650bb5489)/"`
84
+ *
85
+ * @param guid - The input string.
86
+ * @returns The Guid object, or undefined if the string could not be parsed.
87
+ */
88
+ public static tryParse(guid) {
89
+ if (guid) {
90
+ guid = Guid._normalize(guid);
91
+ if (Guid._guidRegEx.test(guid)) {
92
+ return new Guid(guid);
93
+ }
94
+ }
95
+ return undefined;
96
+ }
97
+ /**
98
+ * Indicates whether a GUID is valid, i.e. whether it would be successfully
99
+ * parsed by `Guid.tryParse()`. This function is cheaper than `Guid.tryParse()`
100
+ * because it does not construct a Guid object.
101
+ *
102
+ * @param guid - The input string.
103
+ * @returns true, if the Guid is valid.
104
+ */
105
+ public static isValid(guid) {
106
+ if (guid) {
107
+ guid = Guid._normalize(guid);
108
+ if (Guid._guidRegEx.test(guid)) {
109
+ return true;
110
+ }
111
+ }
112
+ return false;
113
+ }
114
+ /**
115
+ * SharePoint can have guids in the form:
116
+ * - `Guid(...)`
117
+ * - `{...}`
118
+ * _normalize transforms guids in this format to the standard
119
+ * guid format.
120
+ *
121
+ * Example: `Guid(d5369f3b-bd7a-412a-9c0f-7f0650bb5489)` becomes `d5369f3b-bd7a-412a-9c0f-7f0650bb5489`
122
+ * Example: `{d5369f3b-bd7a-412a-9c0f-7f0650bb5489}` becomes `d5369f3b-bd7a-412a-9c0f-7f0650bb5489`
123
+ *
124
+ * @param guid - Guid to be normalized, could already be normalized.
125
+ * @returns Normalized guid.
126
+ */
127
+ private static _normalize(guid) {
128
+ if (isNullOrEmptyString(guid)) {
129
+ throw new Error(`***The value for "guid" must not be an empty string`)
130
+ }
131
+ // Check for Guid(...) and strip
132
+ var result = /^Guid\((.*)\)/i.exec(guid); /* tslint:disable-line:no-null-keyword */
133
+ if (result) {
134
+ guid = result[1];
135
+ }
136
+ // Check for and strip leading or trailing curly braces
137
+ if (guid.length === 38) {
138
+ guid = guid.substr(1, 36);
139
+ }
140
+ return guid.toLowerCase();
141
+ }
142
+ /**
143
+ * Creates a new guid.
144
+ *
145
+ * @returns A valid guid (according to RFC4122)
146
+ */
147
+ private static _generateGuid(randomNumberGenerator) {
148
+ return 'AAAAAAAA-AAAA-4AAA-BAAA-AAAAAAAAAAAA'.replace(/[AB]/g,
149
+ // Callback for String.replace() when generating a guid.
150
+ function (character) {
151
+ var randomNumber = randomNumberGenerator ? randomNumberGenerator.generate() : Math.random();
152
+ /* tslint:disable:no-bitwise */
153
+ var num = randomNumber * 16 | 0;
154
+ // Check for 'A' in template string because the first characters in the
155
+ // third and fourth blocks must be specific characters (according to "version 4" UUID from RFC 4122)
156
+ var masked = character === 'A' ? num : (num & 0x3 | 0x8);
157
+ return masked.toString(16);
158
+ });
159
+ }
160
+ /**
161
+ * Compare this instance to another Guid instance
162
+ *
163
+ * @returns True if this instance and the specified Guid object
164
+ * represent the same value.
165
+ */
166
+ public equals(guid) {
167
+ if (isNullOrUndefined(guid)) {
168
+ throw new Error(`"***The value for "guid" must not be null or undefined"`)
169
+ }
170
+ return guid && this._guid.toString() === guid.toString();
171
+ }
172
+ /**
173
+ * Returns a string representation of the GUID
174
+ *
175
+ * @returns The GUID value in lowercase hexadecimal without braces.
176
+ *
177
+ * Example: `'d5369f3b-bd7a-412a-9c0f-7f0650bb5489'`
178
+ */
179
+ public toString = function () {
180
+ return this._guid;
181
+ }
182
182
  }