@kwiz/common 1.0.128 → 1.0.132

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 (105) hide show
  1. package/.github/workflows/npm-publish.yml +24 -24
  2. package/LICENSE +21 -21
  3. package/lib/cjs/config.js +2 -0
  4. package/lib/cjs/config.js.map +1 -1
  5. package/lib/cjs/helpers/browser.js +8 -1
  6. package/lib/cjs/helpers/browser.js.map +1 -1
  7. package/lib/cjs/types/libs/msal.types.js +26 -26
  8. package/lib/cjs/utils/sharepoint.rest/user.js +11 -11
  9. package/lib/esm/config.js +2 -0
  10. package/lib/esm/config.js.map +1 -1
  11. package/lib/esm/helpers/browser.js +6 -0
  12. package/lib/esm/helpers/browser.js.map +1 -1
  13. package/lib/esm/types/libs/msal.types.js +26 -26
  14. package/lib/esm/utils/sharepoint.rest/user.js +11 -11
  15. package/lib/types/config.d.ts +2 -0
  16. package/lib/types/helpers/browser.d.ts +2 -0
  17. package/package.json +81 -81
  18. package/readme.md +17 -17
  19. package/src/_dependencies.ts +12 -12
  20. package/src/config.ts +19 -17
  21. package/src/helpers/Guid.ts +181 -181
  22. package/src/helpers/base64.ts +173 -173
  23. package/src/helpers/browser.test.js +13 -13
  24. package/src/helpers/browser.ts +1504 -1498
  25. package/src/helpers/browserinfo.ts +292 -292
  26. package/src/helpers/collections.base.test.js +25 -25
  27. package/src/helpers/collections.base.ts +437 -437
  28. package/src/helpers/collections.ts +107 -107
  29. package/src/helpers/color.ts +54 -54
  30. package/src/helpers/cookies.ts +59 -59
  31. package/src/helpers/date.test.js +119 -119
  32. package/src/helpers/date.ts +188 -188
  33. package/src/helpers/debug.ts +186 -186
  34. package/src/helpers/diagrams.ts +43 -43
  35. package/src/helpers/emails.ts +6 -6
  36. package/src/helpers/eval.ts +5 -5
  37. package/src/helpers/file.test.js +50 -50
  38. package/src/helpers/file.ts +63 -63
  39. package/src/helpers/flatted.ts +149 -149
  40. package/src/helpers/functions.ts +16 -16
  41. package/src/helpers/graph/calendar.types.ts +10 -10
  42. package/src/helpers/http.ts +69 -69
  43. package/src/helpers/images.ts +22 -22
  44. package/src/helpers/json.ts +44 -44
  45. package/src/helpers/md5.ts +189 -189
  46. package/src/helpers/objects.test.js +33 -33
  47. package/src/helpers/objects.ts +274 -274
  48. package/src/helpers/promises.test.js +37 -37
  49. package/src/helpers/promises.ts +165 -165
  50. package/src/helpers/random.ts +27 -27
  51. package/src/helpers/scheduler/scheduler.test.js +103 -103
  52. package/src/helpers/scheduler/scheduler.ts +131 -131
  53. package/src/helpers/sharepoint.ts +796 -796
  54. package/src/helpers/strings.test.js +122 -122
  55. package/src/helpers/strings.ts +337 -337
  56. package/src/helpers/typecheckers.test.js +34 -34
  57. package/src/helpers/typecheckers.ts +266 -266
  58. package/src/helpers/url.test.js +43 -43
  59. package/src/helpers/url.ts +207 -207
  60. package/src/helpers/urlhelper.ts +111 -111
  61. package/src/index.ts +6 -6
  62. package/src/types/auth.ts +62 -62
  63. package/src/types/common.types.ts +15 -15
  64. package/src/types/flatted.types.ts +59 -59
  65. package/src/types/globals.types.ts +6 -6
  66. package/src/types/graph/calendar.types.ts +80 -80
  67. package/src/types/knownscript.types.ts +18 -18
  68. package/src/types/libs/datajs.types.ts +28 -28
  69. package/src/types/libs/ics.types.ts +30 -30
  70. package/src/types/libs/msal.types.ts +57 -57
  71. package/src/types/locales.ts +125 -125
  72. package/src/types/localstoragecache.types.ts +8 -8
  73. package/src/types/location.types.ts +27 -27
  74. package/src/types/moment.ts +11 -11
  75. package/src/types/regex.types.ts +16 -16
  76. package/src/types/rest.types.ts +95 -95
  77. package/src/types/sharepoint.types.ts +1466 -1466
  78. package/src/types/sharepoint.utils.types.ts +306 -306
  79. package/src/utils/auth/common.ts +118 -118
  80. package/src/utils/auth/discovery.test.js +12 -12
  81. package/src/utils/auth/discovery.ts +132 -132
  82. package/src/utils/base64.ts +27 -27
  83. package/src/utils/consolelogger.ts +333 -333
  84. package/src/utils/date.ts +172 -172
  85. package/src/utils/emails.ts +24 -24
  86. package/src/utils/knownscript.ts +286 -286
  87. package/src/utils/localstoragecache.ts +446 -446
  88. package/src/utils/rest.ts +501 -501
  89. package/src/utils/script.ts +170 -170
  90. package/src/utils/sharepoint.rest/common.ts +159 -159
  91. package/src/utils/sharepoint.rest/date.ts +62 -62
  92. package/src/utils/sharepoint.rest/file.folder.ts +685 -685
  93. package/src/utils/sharepoint.rest/item.ts +547 -547
  94. package/src/utils/sharepoint.rest/list.ts +1572 -1572
  95. package/src/utils/sharepoint.rest/listutils/GetListItemsByCaml.ts +774 -774
  96. package/src/utils/sharepoint.rest/listutils/GetListItemsById.ts +275 -275
  97. package/src/utils/sharepoint.rest/listutils/common.ts +206 -206
  98. package/src/utils/sharepoint.rest/location.ts +141 -141
  99. package/src/utils/sharepoint.rest/navigation-links.ts +86 -86
  100. package/src/utils/sharepoint.rest/user-search.ts +252 -252
  101. package/src/utils/sharepoint.rest/user.ts +558 -558
  102. package/src/utils/sharepoint.rest/web.ts +1384 -1384
  103. package/src/utils/sod.ts +194 -194
  104. package/.madgerc +0 -3
  105. package/fix-folder-imports.js +0 -27
@@ -1,160 +1,160 @@
1
- import { jsonParse } from "../../helpers/json";
2
- import { isNullOrEmptyString, isNullOrUndefined, isString, isValidGuid } from "../../helpers/typecheckers";
3
- import { makeServerRelativeUrl, normalizeUrl } from "../../helpers/url";
4
- import { IDictionary } from "../../types/common.types";
5
- import { IRestError } from "../../types/rest.types";
6
- import { FieldTypeAsString, IFieldInfoEX, IFieldTaxonomyInfo } from "../../types/sharepoint.types";
7
- import { ISPRestError } from "../../types/sharepoint.utils.types";
8
- import { ConsoleLogger } from "../consolelogger";
9
- import { getCacheItem, setCacheItem } from "../localstoragecache";
10
- import { mediumLocalCache } from "../rest";
11
- import { GetWebIdSync, GetWebInfoSync } from "./web";
12
-
13
- const logger = ConsoleLogger.get("sharepoint.rest/common");
14
-
15
- export const LIST_SELECT = `ListExperienceOptions,EffectiveBasePermissions,Description,Title,EnableAttachments,EnableModeration,BaseTemplate,BaseType,Id,Hidden,IsApplicationList,IsPrivate,IsCatalog,ImageUrl,ItemCount,ParentWebUrl,EntityTypeName,DefaultViewUrl,ParentWeb/Id,ParentWeb/Title`;
16
- export const LIST_EXPAND = `ParentWeb/Id,ParentWeb/Title`;
17
- export const WEB_SELECT = "Title,ServerRelativeUrl,Id,WebTemplate,Description,SiteLogoUrl";
18
- export const CONTENT_TYPES_SELECT = "Name,Description,StringId,Group,Hidden,ReadOnly,NewFormUrl,DisplayFormUrl,EditFormUrl,Sealed,MobileDisplayFormUrl,MobileNewFormUrl,MobileEditFormUrl,NewFormTemplateName,DisplayFormTemplateName,EditFormTemplateName";
19
- export const CONTENT_TYPES_SELECT_WITH_FIELDS = `${CONTENT_TYPES_SELECT},Fields`;
20
-
21
- export function hasGlobalContext() {
22
- //_spPageContextInfo.webServerRelativeUrl can be empty string
23
- return typeof (_spPageContextInfo) !== "undefined" && isString(_spPageContextInfo.webServerRelativeUrl);
24
- }
25
-
26
- export function GetFileSiteUrl(fileUrl: string): string {
27
- let siteUrl: string;
28
- let urlParts = fileUrl.split('/');
29
- if (urlParts[urlParts.length - 1].indexOf('.') > 0)//file name
30
- urlParts.pop();//file name
31
-
32
- let key = "GetSiteUrl|" + urlParts.join("/").toLowerCase();
33
- siteUrl = getCacheItem<string>(key);
34
- if (isNullOrUndefined(siteUrl)) {
35
- while (!isValidGuid(GetWebIdSync(urlParts.join('/'))))
36
- urlParts.pop();
37
-
38
- siteUrl = normalizeUrl(urlParts.join('/'));
39
- setCacheItem(key, siteUrl, mediumLocalCache.localStorageExpiration);//keep for 15 minutes
40
- }
41
- //must end with / otherwise root sites will return "" and we will think there is no site url.
42
- return makeServerRelativeUrl(normalizeUrl(siteUrl, true));
43
- }
44
-
45
- /** gets a site URL or null, returns the current web URL or siteUrl as relative URL - end with /
46
- * If you send a guid - it will look for a site with that ID in the current context site collection
47
- */
48
- export function GetSiteUrl(siteUrlOrId?: string): string {
49
- let siteUrl: string;
50
- if (isNullOrUndefined(siteUrlOrId)) {
51
- if (hasGlobalContext()) {
52
- siteUrl = _spPageContextInfo.webServerRelativeUrl;
53
- if (_spPageContextInfo.isAppWeb)//#1300 if in a classic app sub-site
54
- siteUrl = siteUrl.substring(0, siteUrl.lastIndexOf("/"));
55
- }
56
- else {
57
- siteUrl = GetFileSiteUrl(window.location.pathname);
58
- }
59
- }
60
- else if (isValidGuid(siteUrlOrId)) {
61
- //GetWebInfoSync calls GetSiteUrl recursively, but with null should not get in here
62
- let webInfo = GetWebInfoSync(null, siteUrlOrId);
63
- siteUrl = webInfo.ServerRelativeUrl;
64
- }
65
- else siteUrl = siteUrlOrId;
66
-
67
- //must end with / otherwise root sites will return "" and we will think there is no site url.
68
- return makeServerRelativeUrl(normalizeUrl(siteUrl, true));
69
- }
70
-
71
- /** gets a site url, returns its REST _api url */
72
- export function GetRestBaseUrl(siteUrl: string): string {
73
- siteUrl = GetSiteUrl(siteUrl);
74
- return siteUrl + '_api';
75
- }
76
-
77
- /** Get the field internal name as you can find it in item.FieldValuesAsText[name] (Or FieldValuesForEdit) */
78
- export function DecodeFieldValuesAsTextKey(key: string): string {
79
- return key.replace(/_x005f_/g, "_").replace('OData__', '_');
80
- }
81
-
82
- /** Replaces _ with _x005f_, except OData_ at the start */
83
- export function EncodeFieldValuesAsTextKey(key: string): string {
84
- return key.replace('OData_', '~').replace(/_/g, "_x005f_").replace('~', 'OData_');
85
- }
86
-
87
- /** Gets REST FieldValuesAsText or FieldValuesForEdit and fix their column names so that you can get a field value by its internal name */
88
- export function DecodeFieldValuesAsText(FieldValuesAsText: IDictionary<string>) {
89
- return DecodeFieldValuesForEdit(FieldValuesAsText);
90
- }
91
- /** Gets REST FieldValuesAsText or FieldValuesForEdit and fix their column names so that you can get a field value by its internal name */
92
- export function DecodeFieldValuesForEdit(FieldValuesForEdit: IDictionary<string>) {
93
- let result: IDictionary<string> = {};
94
- Object.keys(FieldValuesForEdit).forEach(key => {
95
- result[DecodeFieldValuesAsTextKey(key)] = FieldValuesForEdit[key];
96
- });
97
- return result;
98
- }
99
-
100
- /** Get the field internal name as you can find it in the item[name] to get raw values */
101
- export function GetFieldNameFromRawValues(
102
- field: { InternalName: string; TypeAsString: FieldTypeAsString; },
103
- //ISSUE: 1250
104
- options: {
105
- excludeIdFromName: boolean
106
- } = {
107
- excludeIdFromName: false
108
- }): string {
109
- let fieldName = field.InternalName;
110
- if (options.excludeIdFromName !== true && (field.TypeAsString === "User" ||
111
- field.TypeAsString === "UserMulti" ||
112
- field.TypeAsString === "Lookup" ||
113
- field.TypeAsString === "LookupMulti" ||
114
- field.InternalName === "ContentType")) {
115
- fieldName = fieldName += "Id";
116
- }
117
-
118
- //issue 6698 fields that are too short will encode their first letter, and will start with _. this will add OData_ as a prefix in REST
119
- //Issue 336 _EndDate > OData__EndDate
120
- if (fieldName.startsWith('_')) {
121
- fieldName = "OData_" + fieldName;
122
- }
123
- return fieldName;
124
- }
125
-
126
- /** Get the field name to set on the item update REST request */
127
- export function getFieldNameForUpdate(field: IFieldInfoEX): string {
128
- if (field.TypeAsString === "TaxonomyFieldTypeMulti") {
129
- //Updating multi taxonomy value is allowed as string to the associated hidden text field
130
- return (field as IFieldTaxonomyInfo).HiddenMultiValueFieldName;
131
- }
132
-
133
- return GetFieldNameFromRawValues(field);
134
- }
135
-
136
- export function __isIRestError(e: any): e is IRestError {
137
- let x = e as IRestError;
138
- return !isNullOrUndefined(x) && !isNullOrUndefined(x.xhr) && isString(x.message);
139
- }
140
- /** extract the error message from a SharePoint REST failed request */
141
- export function __getSPRestErrorData(restError: IRestError) {
142
- let code = "Unknown";
143
- let errorMessage = "Unspecified error";
144
- if (restError && restError.message) errorMessage = restError.message;
145
- if (restError && restError.xhr && !isNullOrEmptyString(restError.xhr.responseText)) {
146
- let errorData = jsonParse<{ error: { code: string; message: { value: string; }; }; }>(restError.xhr.responseText);
147
- let error = errorData && errorData.error;
148
- if (!error && errorData)//in minimal rest - error is in "odata.error"
149
- error = errorData && errorData["odata.error"];
150
-
151
- if (error) {
152
- if (error && error.message && error.message.value)
153
- errorMessage = error.message.value;
154
- if (error && error.code)
155
- code = error.code;
156
- }
157
- }
158
- logger.error(errorMessage);
159
- return { code: code, message: errorMessage } as ISPRestError;
1
+ import { jsonParse } from "../../helpers/json";
2
+ import { isNullOrEmptyString, isNullOrUndefined, isString, isValidGuid } from "../../helpers/typecheckers";
3
+ import { makeServerRelativeUrl, normalizeUrl } from "../../helpers/url";
4
+ import { IDictionary } from "../../types/common.types";
5
+ import { IRestError } from "../../types/rest.types";
6
+ import { FieldTypeAsString, IFieldInfoEX, IFieldTaxonomyInfo } from "../../types/sharepoint.types";
7
+ import { ISPRestError } from "../../types/sharepoint.utils.types";
8
+ import { ConsoleLogger } from "../consolelogger";
9
+ import { getCacheItem, setCacheItem } from "../localstoragecache";
10
+ import { mediumLocalCache } from "../rest";
11
+ import { GetWebIdSync, GetWebInfoSync } from "./web";
12
+
13
+ const logger = ConsoleLogger.get("sharepoint.rest/common");
14
+
15
+ export const LIST_SELECT = `ListExperienceOptions,EffectiveBasePermissions,Description,Title,EnableAttachments,EnableModeration,BaseTemplate,BaseType,Id,Hidden,IsApplicationList,IsPrivate,IsCatalog,ImageUrl,ItemCount,ParentWebUrl,EntityTypeName,DefaultViewUrl,ParentWeb/Id,ParentWeb/Title`;
16
+ export const LIST_EXPAND = `ParentWeb/Id,ParentWeb/Title`;
17
+ export const WEB_SELECT = "Title,ServerRelativeUrl,Id,WebTemplate,Description,SiteLogoUrl";
18
+ export const CONTENT_TYPES_SELECT = "Name,Description,StringId,Group,Hidden,ReadOnly,NewFormUrl,DisplayFormUrl,EditFormUrl,Sealed,MobileDisplayFormUrl,MobileNewFormUrl,MobileEditFormUrl,NewFormTemplateName,DisplayFormTemplateName,EditFormTemplateName";
19
+ export const CONTENT_TYPES_SELECT_WITH_FIELDS = `${CONTENT_TYPES_SELECT},Fields`;
20
+
21
+ export function hasGlobalContext() {
22
+ //_spPageContextInfo.webServerRelativeUrl can be empty string
23
+ return typeof (_spPageContextInfo) !== "undefined" && isString(_spPageContextInfo.webServerRelativeUrl);
24
+ }
25
+
26
+ export function GetFileSiteUrl(fileUrl: string): string {
27
+ let siteUrl: string;
28
+ let urlParts = fileUrl.split('/');
29
+ if (urlParts[urlParts.length - 1].indexOf('.') > 0)//file name
30
+ urlParts.pop();//file name
31
+
32
+ let key = "GetSiteUrl|" + urlParts.join("/").toLowerCase();
33
+ siteUrl = getCacheItem<string>(key);
34
+ if (isNullOrUndefined(siteUrl)) {
35
+ while (!isValidGuid(GetWebIdSync(urlParts.join('/'))))
36
+ urlParts.pop();
37
+
38
+ siteUrl = normalizeUrl(urlParts.join('/'));
39
+ setCacheItem(key, siteUrl, mediumLocalCache.localStorageExpiration);//keep for 15 minutes
40
+ }
41
+ //must end with / otherwise root sites will return "" and we will think there is no site url.
42
+ return makeServerRelativeUrl(normalizeUrl(siteUrl, true));
43
+ }
44
+
45
+ /** gets a site URL or null, returns the current web URL or siteUrl as relative URL - end with /
46
+ * If you send a guid - it will look for a site with that ID in the current context site collection
47
+ */
48
+ export function GetSiteUrl(siteUrlOrId?: string): string {
49
+ let siteUrl: string;
50
+ if (isNullOrUndefined(siteUrlOrId)) {
51
+ if (hasGlobalContext()) {
52
+ siteUrl = _spPageContextInfo.webServerRelativeUrl;
53
+ if (_spPageContextInfo.isAppWeb)//#1300 if in a classic app sub-site
54
+ siteUrl = siteUrl.substring(0, siteUrl.lastIndexOf("/"));
55
+ }
56
+ else {
57
+ siteUrl = GetFileSiteUrl(window.location.pathname);
58
+ }
59
+ }
60
+ else if (isValidGuid(siteUrlOrId)) {
61
+ //GetWebInfoSync calls GetSiteUrl recursively, but with null should not get in here
62
+ let webInfo = GetWebInfoSync(null, siteUrlOrId);
63
+ siteUrl = webInfo.ServerRelativeUrl;
64
+ }
65
+ else siteUrl = siteUrlOrId;
66
+
67
+ //must end with / otherwise root sites will return "" and we will think there is no site url.
68
+ return makeServerRelativeUrl(normalizeUrl(siteUrl, true));
69
+ }
70
+
71
+ /** gets a site url, returns its REST _api url */
72
+ export function GetRestBaseUrl(siteUrl: string): string {
73
+ siteUrl = GetSiteUrl(siteUrl);
74
+ return siteUrl + '_api';
75
+ }
76
+
77
+ /** Get the field internal name as you can find it in item.FieldValuesAsText[name] (Or FieldValuesForEdit) */
78
+ export function DecodeFieldValuesAsTextKey(key: string): string {
79
+ return key.replace(/_x005f_/g, "_").replace('OData__', '_');
80
+ }
81
+
82
+ /** Replaces _ with _x005f_, except OData_ at the start */
83
+ export function EncodeFieldValuesAsTextKey(key: string): string {
84
+ return key.replace('OData_', '~').replace(/_/g, "_x005f_").replace('~', 'OData_');
85
+ }
86
+
87
+ /** Gets REST FieldValuesAsText or FieldValuesForEdit and fix their column names so that you can get a field value by its internal name */
88
+ export function DecodeFieldValuesAsText(FieldValuesAsText: IDictionary<string>) {
89
+ return DecodeFieldValuesForEdit(FieldValuesAsText);
90
+ }
91
+ /** Gets REST FieldValuesAsText or FieldValuesForEdit and fix their column names so that you can get a field value by its internal name */
92
+ export function DecodeFieldValuesForEdit(FieldValuesForEdit: IDictionary<string>) {
93
+ let result: IDictionary<string> = {};
94
+ Object.keys(FieldValuesForEdit).forEach(key => {
95
+ result[DecodeFieldValuesAsTextKey(key)] = FieldValuesForEdit[key];
96
+ });
97
+ return result;
98
+ }
99
+
100
+ /** Get the field internal name as you can find it in the item[name] to get raw values */
101
+ export function GetFieldNameFromRawValues(
102
+ field: { InternalName: string; TypeAsString: FieldTypeAsString; },
103
+ //ISSUE: 1250
104
+ options: {
105
+ excludeIdFromName: boolean
106
+ } = {
107
+ excludeIdFromName: false
108
+ }): string {
109
+ let fieldName = field.InternalName;
110
+ if (options.excludeIdFromName !== true && (field.TypeAsString === "User" ||
111
+ field.TypeAsString === "UserMulti" ||
112
+ field.TypeAsString === "Lookup" ||
113
+ field.TypeAsString === "LookupMulti" ||
114
+ field.InternalName === "ContentType")) {
115
+ fieldName = fieldName += "Id";
116
+ }
117
+
118
+ //issue 6698 fields that are too short will encode their first letter, and will start with _. this will add OData_ as a prefix in REST
119
+ //Issue 336 _EndDate > OData__EndDate
120
+ if (fieldName.startsWith('_')) {
121
+ fieldName = "OData_" + fieldName;
122
+ }
123
+ return fieldName;
124
+ }
125
+
126
+ /** Get the field name to set on the item update REST request */
127
+ export function getFieldNameForUpdate(field: IFieldInfoEX): string {
128
+ if (field.TypeAsString === "TaxonomyFieldTypeMulti") {
129
+ //Updating multi taxonomy value is allowed as string to the associated hidden text field
130
+ return (field as IFieldTaxonomyInfo).HiddenMultiValueFieldName;
131
+ }
132
+
133
+ return GetFieldNameFromRawValues(field);
134
+ }
135
+
136
+ export function __isIRestError(e: any): e is IRestError {
137
+ let x = e as IRestError;
138
+ return !isNullOrUndefined(x) && !isNullOrUndefined(x.xhr) && isString(x.message);
139
+ }
140
+ /** extract the error message from a SharePoint REST failed request */
141
+ export function __getSPRestErrorData(restError: IRestError) {
142
+ let code = "Unknown";
143
+ let errorMessage = "Unspecified error";
144
+ if (restError && restError.message) errorMessage = restError.message;
145
+ if (restError && restError.xhr && !isNullOrEmptyString(restError.xhr.responseText)) {
146
+ let errorData = jsonParse<{ error: { code: string; message: { value: string; }; }; }>(restError.xhr.responseText);
147
+ let error = errorData && errorData.error;
148
+ if (!error && errorData)//in minimal rest - error is in "odata.error"
149
+ error = errorData && errorData["odata.error"];
150
+
151
+ if (error) {
152
+ if (error && error.message && error.message.value)
153
+ errorMessage = error.message.value;
154
+ if (error && error.code)
155
+ code = error.code;
156
+ }
157
+ }
158
+ logger.error(errorMessage);
159
+ return { code: code, message: errorMessage } as ISPRestError;
160
160
  }
@@ -1,63 +1,63 @@
1
- import { isISODate, isISODateUTC } from "../../helpers/date";
2
- import { isDate, isNullOrEmptyString, isNullOrUndefined } from "../../helpers/typecheckers";
3
- import { DateOrNull } from "../../types/common.types";
4
- import { toIsoDateFormat } from "../date";
5
- import { LocaleKnownScript } from "../knownscript";
6
- import { SPServerLocalTimeToUTCSync } from "./web";
7
-
8
- function _SPServerLocalTimeToUTC(dateValueStr: string) {
9
- //yyyy-MM-ddTHH:mm or SPServerLocalTime (5/27/2021 11:34) to UTC
10
- let utcTimeValue = SPServerLocalTimeToUTCSync(_spPageContextInfo.webServerRelativeUrl, dateValueStr);
11
-
12
- if (!isNullOrEmptyString(utcTimeValue)) {
13
- return new Date(utcTimeValue);
14
- }
15
- return null;
16
- }
17
-
18
- export function parseDate(value: string | Date): DateOrNull {
19
- if (isNullOrUndefined(value) || (isNullOrEmptyString(value) && !isDate(value))) {
20
- return null;
21
- }
22
-
23
- let dateValueStr = isDate(value) ? value.toISOString() : value;
24
-
25
- var valueAsDate: DateOrNull = null;
26
-
27
- if (isISODateUTC(dateValueStr)) {
28
- valueAsDate = new Date(dateValueStr);
29
- } else if (isISODate(dateValueStr)) {
30
- //yyyy-MM-ddTHH:mm or SPServerLocalTime (5/27/2021 11:34) to UTC
31
- valueAsDate = _SPServerLocalTimeToUTC(dateValueStr);
32
- } else {
33
- //Don't think this is necessary because the SPServerLocalTimeToUTCSync can accept date strings in non ISO format (5/27/2021 11:34)
34
- var dateParts: string[] = dateValueStr.split(" ");
35
- if (dateParts.length) {
36
- let locales = LocaleKnownScript.loadSync();
37
-
38
- valueAsDate = locales.ParseDate(dateValueStr);
39
- dateValueStr = toIsoDateFormat(valueAsDate, { omitZ: true });
40
- valueAsDate = _SPServerLocalTimeToUTC(dateValueStr);
41
-
42
- if (valueAsDate instanceof Date && dateParts.length === 2) {
43
- var time = dateParts[1];
44
- if (time.length) {
45
- var timeParts = locales.GetCurrentCulture() ? locales.GetCurrentCulture().dateTimeFormat.TimeSeparator : ":";
46
- if (timeParts.length === 2) {
47
- var hours = Number(timeParts[0]);
48
- var minutes = Number(timeParts[1]);
49
-
50
- valueAsDate.setHours(hours);
51
- valueAsDate.setMinutes(minutes);
52
- }
53
- }
54
- }
55
- }
56
- }
57
-
58
- if (!valueAsDate) {
59
- valueAsDate = new Date(dateValueStr);
60
- }
61
-
62
- return isDate(valueAsDate) ? valueAsDate : null;
1
+ import { isISODate, isISODateUTC } from "../../helpers/date";
2
+ import { isDate, isNullOrEmptyString, isNullOrUndefined } from "../../helpers/typecheckers";
3
+ import { DateOrNull } from "../../types/common.types";
4
+ import { toIsoDateFormat } from "../date";
5
+ import { LocaleKnownScript } from "../knownscript";
6
+ import { SPServerLocalTimeToUTCSync } from "./web";
7
+
8
+ function _SPServerLocalTimeToUTC(dateValueStr: string) {
9
+ //yyyy-MM-ddTHH:mm or SPServerLocalTime (5/27/2021 11:34) to UTC
10
+ let utcTimeValue = SPServerLocalTimeToUTCSync(_spPageContextInfo.webServerRelativeUrl, dateValueStr);
11
+
12
+ if (!isNullOrEmptyString(utcTimeValue)) {
13
+ return new Date(utcTimeValue);
14
+ }
15
+ return null;
16
+ }
17
+
18
+ export function parseDate(value: string | Date): DateOrNull {
19
+ if (isNullOrUndefined(value) || (isNullOrEmptyString(value) && !isDate(value))) {
20
+ return null;
21
+ }
22
+
23
+ let dateValueStr = isDate(value) ? value.toISOString() : value;
24
+
25
+ var valueAsDate: DateOrNull = null;
26
+
27
+ if (isISODateUTC(dateValueStr)) {
28
+ valueAsDate = new Date(dateValueStr);
29
+ } else if (isISODate(dateValueStr)) {
30
+ //yyyy-MM-ddTHH:mm or SPServerLocalTime (5/27/2021 11:34) to UTC
31
+ valueAsDate = _SPServerLocalTimeToUTC(dateValueStr);
32
+ } else {
33
+ //Don't think this is necessary because the SPServerLocalTimeToUTCSync can accept date strings in non ISO format (5/27/2021 11:34)
34
+ var dateParts: string[] = dateValueStr.split(" ");
35
+ if (dateParts.length) {
36
+ let locales = LocaleKnownScript.loadSync();
37
+
38
+ valueAsDate = locales.ParseDate(dateValueStr);
39
+ dateValueStr = toIsoDateFormat(valueAsDate, { omitZ: true });
40
+ valueAsDate = _SPServerLocalTimeToUTC(dateValueStr);
41
+
42
+ if (valueAsDate instanceof Date && dateParts.length === 2) {
43
+ var time = dateParts[1];
44
+ if (time.length) {
45
+ var timeParts = locales.GetCurrentCulture() ? locales.GetCurrentCulture().dateTimeFormat.TimeSeparator : ":";
46
+ if (timeParts.length === 2) {
47
+ var hours = Number(timeParts[0]);
48
+ var minutes = Number(timeParts[1]);
49
+
50
+ valueAsDate.setHours(hours);
51
+ valueAsDate.setMinutes(minutes);
52
+ }
53
+ }
54
+ }
55
+ }
56
+ }
57
+
58
+ if (!valueAsDate) {
59
+ valueAsDate = new Date(dateValueStr);
60
+ }
61
+
62
+ return isDate(valueAsDate) ? valueAsDate : null;
63
63
  }