@vonq/hapi-elements-types 1.4.0 → 1.5.0

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 (188) hide show
  1. package/enums.ts +41 -0
  2. package/package.json +5 -7
  3. package/src/_window/api.types.ts +59 -0
  4. package/src/_window/auth.types.ts +11 -0
  5. package/src/_window/config.types.ts +30 -0
  6. package/src/_window/events.types.ts +157 -0
  7. package/src/_window/index.ts +16 -0
  8. package/src/_window/instances.types.ts +18 -0
  9. package/src/_window/qa.types.ts +28 -0
  10. package/src/_window/routing.types.ts +21 -0
  11. package/src/_window/sdk.types.ts +147 -0
  12. package/src/_window/service.types.ts +67 -0
  13. package/src/_window/state.types.ts +112 -0
  14. package/src/_window/ui.types.ts +13 -0
  15. package/src/_window/utils.types.ts +34 -0
  16. package/src/_window/validation.types.ts +43 -0
  17. package/src/_window/window.ts +56 -0
  18. package/src/alert/enums.ts +22 -0
  19. package/src/alert/functions.types.ts +13 -0
  20. package/src/alert/index.ts +9 -0
  21. package/src/alert/service.types.ts +15 -0
  22. package/src/alert/state.initial.ts +16 -0
  23. package/src/alert/state.types.ts +27 -0
  24. package/src/alert/state.validations.ts +11 -0
  25. package/src/alert/types.ts +63 -0
  26. package/src/ats/api.types.ts +16 -0
  27. package/src/ats/functions.types.ts +7 -0
  28. package/src/ats/index.ts +9 -0
  29. package/src/ats/service.types.ts +17 -0
  30. package/src/ats/state.initial.ts +5 -0
  31. package/src/ats/state.types.ts +5 -0
  32. package/src/ats/state.validations.ts +6 -0
  33. package/src/ats/types.ts +19 -0
  34. package/src/basket/consts.ts +1 -0
  35. package/src/basket/functions.types.ts +40 -0
  36. package/src/basket/index.ts +12 -0
  37. package/src/basket/service.types.ts +32 -0
  38. package/src/basket/state.initial.ts +11 -0
  39. package/src/basket/state.types.ts +34 -0
  40. package/src/basket/state.validations.ts +12 -0
  41. package/src/basket/types.ts +4 -0
  42. package/src/basket/utils.ts +79 -0
  43. package/src/basket/utils.types.ts +10 -0
  44. package/src/basket/validations.types.ts +25 -0
  45. package/src/campaign/api.types.ts +45 -0
  46. package/src/campaign/consts.ts +1 -0
  47. package/src/campaign/enums.ts +17 -0
  48. package/src/campaign/functions.types.ts +90 -0
  49. package/src/campaign/index.ts +15 -0
  50. package/src/campaign/qa.types.ts +12 -0
  51. package/src/campaign/service.types.ts +48 -0
  52. package/src/campaign/state.initial.ts +84 -0
  53. package/src/campaign/state.types.ts +55 -0
  54. package/src/campaign/state.validations.ts +16 -0
  55. package/src/campaign/types.ts +237 -0
  56. package/src/campaign/utils.ts +53 -0
  57. package/src/campaign/utils.types.ts +59 -0
  58. package/src/campaign/validations.types.ts +215 -0
  59. package/src/common/LocalStorage.ts +123 -0
  60. package/src/common/browser.ts +2 -0
  61. package/src/common/color.ts +9 -0
  62. package/src/common/events/EventCommand/enums.ts +49 -0
  63. package/src/common/events/EventCommand/index.ts +4 -0
  64. package/src/common/events/EventCommand/types.ts +18 -0
  65. package/src/common/events/index.ts +4 -0
  66. package/src/common/events/types.ts +52 -0
  67. package/src/common/immutable.ts +28 -0
  68. package/src/common/index.ts +22 -0
  69. package/src/common/instances/Instance.ts +13 -0
  70. package/src/common/instances/consts.ts +1 -0
  71. package/src/common/instances/index.ts +4 -0
  72. package/src/common/json.ts +25 -0
  73. package/src/common/logger/Logger.ts +70 -0
  74. package/src/common/logger/enums.ts +12 -0
  75. package/src/common/logger/index.ts +5 -0
  76. package/src/common/logger/types.ts +7 -0
  77. package/src/common/logger-datadog/LoggerDatadog.ts +42 -0
  78. package/src/common/logger-datadog/index.ts +3 -0
  79. package/src/common/misc.ts +114 -0
  80. package/src/common/promise.ts +161 -0
  81. package/src/common/proxy.ts +77 -0
  82. package/src/common/qa.types.ts +9 -0
  83. package/src/common/regExTest.ts +12 -0
  84. package/src/common/tree.ts +148 -0
  85. package/src/common/types.ts +110 -0
  86. package/src/common/utm.ts +19 -0
  87. package/src/common/validations.types.ts +89 -0
  88. package/src/common/validators.ts +21 -0
  89. package/src/common/webComponents.ts +26 -0
  90. package/src/config/functions.types.ts +23 -0
  91. package/src/config/index.ts +5 -0
  92. package/src/config/state.initial.ts +11 -0
  93. package/src/config/state.types.ts +15 -0
  94. package/src/consts.ts +980 -0
  95. package/src/contract/api.types.ts +47 -0
  96. package/src/contract/consts.ts +1 -0
  97. package/src/contract/functions.types.ts +112 -0
  98. package/src/contract/index.ts +14 -0
  99. package/src/contract/qa.types.ts +12 -0
  100. package/src/contract/service.types.ts +54 -0
  101. package/src/contract/state.initial.ts +39 -0
  102. package/src/contract/state.types.ts +74 -0
  103. package/src/contract/state.validations.ts +21 -0
  104. package/src/contract/types.ts +145 -0
  105. package/src/contract/utils.ts +36 -0
  106. package/src/contract/utils.types.ts +11 -0
  107. package/src/contract/validations.types.ts +104 -0
  108. package/src/debugging/functions.types.ts +34 -0
  109. package/src/debugging/index.ts +6 -0
  110. package/src/debugging/state.initial.ts +10 -0
  111. package/src/debugging/state.types.ts +18 -0
  112. package/src/debugging/state.validations.ts +10 -0
  113. package/src/index.ts +22 -0
  114. package/src/language/consts.ts +6 -0
  115. package/src/language/en.ts +530 -0
  116. package/src/language/functions.types.ts +29 -0
  117. package/src/language/index.ts +11 -0
  118. package/src/language/qa.types.ts +19 -0
  119. package/src/language/state.initial.ts +13 -0
  120. package/src/language/state.types.ts +25 -0
  121. package/src/language/state.validations.ts +10 -0
  122. package/src/language/utils.ts +17 -0
  123. package/src/language/validations.types.ts +13 -0
  124. package/src/modal/consts.ts +6 -0
  125. package/src/modal/enums.ts +14 -0
  126. package/src/modal/functions.types.ts +22 -0
  127. package/src/modal/index.ts +11 -0
  128. package/src/modal/service.types.ts +32 -0
  129. package/src/modal/state.initial.ts +9 -0
  130. package/src/modal/state.types.ts +8 -0
  131. package/src/modal/state.validations.ts +6 -0
  132. package/src/modal/store.types.ts +5 -0
  133. package/src/modal/types.ts +44 -0
  134. package/src/orderJourney/consts.ts +289 -0
  135. package/src/orderJourney/enums.ts +25 -0
  136. package/src/orderJourney/functions.types.ts +140 -0
  137. package/src/orderJourney/index.ts +13 -0
  138. package/src/orderJourney/qa.types.ts +16 -0
  139. package/src/orderJourney/state.initial.ts +401 -0
  140. package/src/orderJourney/state.types.ts +99 -0
  141. package/src/orderJourney/state.validations.ts +78 -0
  142. package/src/orderJourney/types.ts +31 -0
  143. package/src/orderJourney/utils.ts +7 -0
  144. package/src/orderJourney/utils.types.ts +11 -0
  145. package/src/orderJourney/validations.types.ts +34 -0
  146. package/src/product/api.types.ts +62 -0
  147. package/src/product/consts.ts +1 -0
  148. package/src/product/enums.ts +11 -0
  149. package/src/product/functions.types.ts +156 -0
  150. package/src/product/index.ts +14 -0
  151. package/src/product/qa.types.ts +11 -0
  152. package/src/product/service.types.ts +68 -0
  153. package/src/product/state.initial.ts +28 -0
  154. package/src/product/state.types.ts +93 -0
  155. package/src/product/state.validations.ts +25 -0
  156. package/src/product/types.ts +161 -0
  157. package/src/product/utils.types.ts +22 -0
  158. package/src/product/validations.types.ts +189 -0
  159. package/src/routing/functions.types.ts +48 -0
  160. package/src/routing/index.ts +5 -0
  161. package/src/routing/state.initial.ts +10 -0
  162. package/src/routing/state.types.ts +30 -0
  163. package/src/theming/consts.ts +7727 -0
  164. package/src/theming/functions.types.ts +13 -0
  165. package/src/theming/index.ts +11 -0
  166. package/src/theming/qa.types.ts +11 -0
  167. package/src/theming/state.initial.ts +16 -0
  168. package/src/theming/state.types.ts +6 -0
  169. package/src/theming/state.validations.ts +8 -0
  170. package/src/theming/types.ts +123 -0
  171. package/src/theming/utils.ts +581 -0
  172. package/src/theming/validations.types.ts +62 -0
  173. package/src/types.ts +229 -0
  174. package/src/ui/index.ts +3 -0
  175. package/src/ui/service.types.ts +26 -0
  176. package/src/utils.ts +3 -0
  177. package/src/wallet/api.types.ts +33 -0
  178. package/src/wallet/consts.ts +3 -0
  179. package/src/wallet/functions.types.ts +58 -0
  180. package/src/wallet/index.ts +11 -0
  181. package/src/wallet/service.types.ts +26 -0
  182. package/src/wallet/state.initial.ts +18 -0
  183. package/src/wallet/state.types.ts +58 -0
  184. package/src/wallet/state.validations.ts +18 -0
  185. package/src/wallet/types.ts +65 -0
  186. package/src/wallet/validations.types.ts +18 -0
  187. package/tsconfig.json +41 -0
  188. package/index.d.ts +0 -5207
@@ -0,0 +1,49 @@
1
+ export enum WindowHapiEventCommandName {
2
+ /* State */
3
+ stateHydrate = "state:hydrate",
4
+ stateTransfer = "state:transfer",
5
+ stateRestore = "state:restore",
6
+ stateLoad = "state:load",
7
+ stateInit = "state:init",
8
+
9
+ /* Service */
10
+ serviceLoad = "service:load",
11
+ serviceInit = "service:init",
12
+ serviceRequest = "service:request",
13
+ serviceStart = "service:start",
14
+ serviceSuccess = "service:success",
15
+ serviceFailure = "service:failure",
16
+ serviceFinish = "service:finish",
17
+
18
+ /* Config */
19
+ configSetPartnerId = "config:partnerId",
20
+ configSetClientId = "config:clientId",
21
+ configSetAreLogsEnabled = "config:areLogsEnabled",
22
+
23
+ /* App */
24
+ appLoad = "app:load",
25
+
26
+ /* API */
27
+ apiRequest = "api:request",
28
+
29
+ /* QA */
30
+ qaRequest = "qa:request",
31
+
32
+ /* DOM */
33
+ domAppendChild = "dom:append-child",
34
+ domElementEvent = "dom:element-event",
35
+ domRemoveChild = "dom:remove-child",
36
+ domHideElement = "dom:hide-element",
37
+ domShowElement = "dom:show-element",
38
+ domSetStyleAttribute = "dom:set-style-attribute",
39
+ domAddClass = "dom:add-class",
40
+ domRemoveClass = "dom:remove-class",
41
+ domSlotAvailable = "dom:slot-available",
42
+
43
+ /* Location */
44
+ locationSetHref = "location:href",
45
+ }
46
+
47
+ export enum EventCommandErrorResponse {
48
+ stateNotFound = "state-undefined-or-not-found",
49
+ }
@@ -0,0 +1,4 @@
1
+ // created from 'create-ts-index'
2
+
3
+ export * from './enums';
4
+ export * from './types';
@@ -0,0 +1,18 @@
1
+ import { WindowHapiEventCommandName } from "./enums"
2
+
3
+ export type WindowHapiEventCommandNames =
4
+ | WindowHapiEventCommandName
5
+ | `dom:element-event-${string}`
6
+
7
+ export type WindowHapiEventCommandData =
8
+ | [string]
9
+ | [string, ...any[]]
10
+ | [...any[]]
11
+ | undefined
12
+
13
+ export type WindowHapiEventCommandStatus =
14
+ | "pending"
15
+ | "dispatched"
16
+ | "resolved"
17
+ | "rejected"
18
+ | "consumed"
@@ -0,0 +1,4 @@
1
+ // created from 'create-ts-index'
2
+
3
+ export * from './EventCommand';
4
+ export * from './types';
@@ -0,0 +1,52 @@
1
+ import {
2
+ WindowHapiEventCommandData,
3
+ WindowHapiEventCommandNames,
4
+ WindowHapiEventCommandStatus,
5
+ } from "./EventCommand/types"
6
+ import { Logger } from "../logger/Logger"
7
+ import HapiInstance from "../instances/Instance"
8
+
9
+ export type WindowHapiEventStrategy = {
10
+ handleEvent: (
11
+ event: WindowHapiEventCommand,
12
+ ) => Promise<WindowHapiEventCommand>
13
+ }
14
+
15
+ export type WindowHapiEventMediatorInstances = {
16
+ source: HapiInstance
17
+ targets: HapiInstance[]
18
+ }
19
+
20
+ export type WindowHapiEventMediatorGetInstancesHandler =
21
+ () => WindowHapiEventMediatorInstances
22
+
23
+ export type WindowHapiEventCommand = {
24
+ id: string
25
+ name: WindowHapiEventCommandNames
26
+ createdAt: string
27
+ updatedAt: string | undefined
28
+ status: WindowHapiEventCommandStatus
29
+ logger: Logger
30
+ instance?: HapiInstance
31
+ data: WindowHapiEventCommandData | undefined
32
+ sourceInstanceName: string
33
+ targetInstancesNames: string[]
34
+ toJSON: Record<string, any>
35
+ }
36
+
37
+ export type WindowHapiEventCommandCallbackHandler = (data: any) => void
38
+ export type WindowHapiEventCommandCommitHandler = (
39
+ ...args: any[]
40
+ ) => void | never | Promise<void | never>
41
+
42
+ export type WindowHapiEventCommandWithCallback = WindowHapiEventCommand & {
43
+ callback?: WindowHapiEventCommandCallbackHandler
44
+ }
45
+
46
+ export type WindowHapiEventListener = {
47
+ id?: string
48
+ name: WindowHapiEventCommandNames
49
+ callback:
50
+ | WindowHapiEventCommandCallbackHandler
51
+ | WindowHapiEventCommandCommitHandler
52
+ }
@@ -0,0 +1,28 @@
1
+ import { isObject } from "./misc"
2
+ import isEqual from "lodash/isEqual"
3
+
4
+ export const mergeDeepOverwriteArrays = (original: any, _overwrites: any) => {
5
+ const output = Object.assign({}, original)
6
+ const overwrites = Object.assign({}, _overwrites)
7
+
8
+ if (isObject(overwrites) && Object.keys(overwrites).length > 0) {
9
+ Object.keys(overwrites).forEach((key) => {
10
+ const value = overwrites[key]
11
+ if (Array.isArray(value) && !isEqual(output[key], value)) {
12
+ output[key] = value
13
+ } else if (
14
+ isObject(value) &&
15
+ !isEqual(
16
+ output[key],
17
+ mergeDeepOverwriteArrays(output[key], value),
18
+ )
19
+ ) {
20
+ output[key] = mergeDeepOverwriteArrays(output[key], value)
21
+ } else if (!isEqual(output[key], value)) {
22
+ output[key] = value
23
+ }
24
+ })
25
+ }
26
+
27
+ return output
28
+ }
@@ -0,0 +1,22 @@
1
+ // created from 'create-ts-index'
2
+
3
+ export * from './events';
4
+ export * from './instances';
5
+ export * from './logger';
6
+ export * from './logger-datadog';
7
+ export * from './LocalStorage';
8
+ export * from './browser';
9
+ export * from './color';
10
+ export * from './immutable';
11
+ export * from './json';
12
+ export * from './misc';
13
+ export * from './promise';
14
+ export * from './proxy';
15
+ export * from './qa.types';
16
+ export * from './regExTest';
17
+ export * from './tree';
18
+ export * from './types';
19
+ export * from './utm';
20
+ export * from './validations.types';
21
+ export * from './validators';
22
+ export * from './webComponents';
@@ -0,0 +1,13 @@
1
+ import { Logger } from "../logger/Logger"
2
+ import { BaseService } from "../types"
3
+
4
+ export default class HapiInstance implements BaseService {
5
+ logger: Logger
6
+ name: string
7
+ window?: Window
8
+ constructor(private _name: string, private _window?: Window) {
9
+ this.name = _name
10
+ this.logger = new Logger(this.name)
11
+ this.window = _window
12
+ }
13
+ }
@@ -0,0 +1 @@
1
+ export const atsInstanceName: string = "ats"
@@ -0,0 +1,4 @@
1
+ // created from 'create-ts-index'
2
+
3
+ export * from './Instance';
4
+ export * from './consts';
@@ -0,0 +1,25 @@
1
+ export const decycle = (obj: any, stack: any[] = []): any => {
2
+ if (!obj || typeof obj !== "object") return obj
3
+
4
+ if (stack.includes(obj)) return null
5
+
6
+ let s = stack.concat([obj])
7
+
8
+ return Array.isArray(obj)
9
+ ? obj.map((x) => decycle(x, s))
10
+ : Object.fromEntries(
11
+ Object.entries(obj)
12
+ .filter(
13
+ ([k]) =>
14
+ k !== "window" &&
15
+ k !== "_window" &&
16
+ k !== "core" &&
17
+ k !== "events" &&
18
+ k !== "state",
19
+ )
20
+ .map(([k, v]) => [
21
+ k.startsWith("_") ? k.replace("_", "") : k,
22
+ decycle(v, s),
23
+ ]),
24
+ )
25
+ }
@@ -0,0 +1,70 @@
1
+ import { LogLevel } from "./enums"
2
+ import { LogOutput } from "./types"
3
+
4
+ export class Logger {
5
+ static level = LogLevel.Debug
6
+ static outputs: LogOutput[] = []
7
+ public id = `logger-${this.source}`
8
+
9
+ constructor(
10
+ public source?: string,
11
+ public isEnabled: boolean = true, // // by default all logs should be disabled, ATS should enable it if they want
12
+ ) {
13
+ /*const areAllLogsEnabled =
14
+ cookie.get(DEBUG_COOKIE_ARE_LOGS_ENABLED) === "true"
15
+
16
+ if (areAllLogsEnabled) {
17
+ this.isEnabled = areAllLogsEnabled
18
+ }*/
19
+ }
20
+
21
+ setIsEnabled = (isEnabled: boolean) => {
22
+ //this.isEnabled = isEnabled
23
+ }
24
+
25
+ static enableProductionMode() {
26
+ Logger.level = LogLevel.Warning
27
+ }
28
+
29
+ debug(...objects: any[]) {
30
+ this.log(console.log, LogLevel.Debug, objects)
31
+ }
32
+
33
+ info(...objects: any[]) {
34
+ this.log(console.info, LogLevel.Info, objects)
35
+ }
36
+
37
+ warn(...objects: any[]) {
38
+ this.log(console.warn, LogLevel.Warning, objects)
39
+ }
40
+
41
+ error(...objects: any[]) {
42
+ this.log(console.error, LogLevel.Error, objects)
43
+ }
44
+
45
+ timeStart(message: string) {
46
+ this.log(console.time, LogLevel.Info, [message])
47
+ }
48
+
49
+ timeEnd(message: string) {
50
+ this.log(console.log, LogLevel.Info, [message, "timer finished"])
51
+ this.log(console.timeEnd, LogLevel.Info, [message])
52
+ }
53
+
54
+ private log(func: Function, level: LogLevel, objects: any[]) {
55
+ if (level <= Logger.level && this.isEnabled) {
56
+ let sourceString = "[HAPI]"
57
+
58
+ sourceString += `[${this.source}]`
59
+
60
+ const log = this.source ? [sourceString].concat(objects) : objects
61
+ func.apply(console, log)
62
+ Logger.outputs.forEach((output) =>
63
+ output.apply(
64
+ output,
65
+ [this.source, level].concat(objects) as any,
66
+ ),
67
+ )
68
+ }
69
+ }
70
+ }
@@ -0,0 +1,12 @@
1
+ export enum LogLevel {
2
+ Off = 0,
3
+ Error,
4
+ Warning,
5
+ Info,
6
+ Debug,
7
+ }
8
+
9
+ export enum NetworkErrorLoggerType {
10
+ "XHR" = "XHR",
11
+ "WS" = "WS",
12
+ }
@@ -0,0 +1,5 @@
1
+ // created from 'create-ts-index'
2
+
3
+ export * from './Logger';
4
+ export * from './enums';
5
+ export * from './types';
@@ -0,0 +1,7 @@
1
+ import { LogLevel } from "./enums"
2
+
3
+ export type LogOutput = (
4
+ source: string,
5
+ level: LogLevel,
6
+ ...objects: any[]
7
+ ) => void
@@ -0,0 +1,42 @@
1
+ import { datadogLogs, Logger } from "@datadog/browser-logs"
2
+ import { HandlerType } from "@datadog/browser-logs/cjs/domain/logger"
3
+
4
+ export default class LoggerDatadog {
5
+ prefix: string
6
+ logger: Logger
7
+ commonContext: Record<string, any> = {}
8
+ handler: HandlerType = "http"
9
+ constructor(
10
+ prefix: string,
11
+ commonContext?: Record<string, any>,
12
+ handler?: HandlerType,
13
+ ) {
14
+ this.prefix = `[${prefix}]`
15
+ this.logger = datadogLogs.createLogger(this.prefix)
16
+ this.commonContext = commonContext || {}
17
+ this.handler = handler || "http"
18
+ }
19
+
20
+ info = (message: string, messageContext?: Record<string, any>) => {
21
+ this.logger.info(message, this.getMergedContext(messageContext))
22
+ }
23
+
24
+ debug = (message: string, messageContext?: Record<string, any>) => {
25
+ this.logger.debug(message, this.getMergedContext(messageContext))
26
+ }
27
+
28
+ error = (message: string, messageContext?: Record<string, any>) => {
29
+ this.logger.error(message, this.getMergedContext(messageContext))
30
+ }
31
+
32
+ warn = (message: string, messageContext?: Record<string, any>) => {
33
+ this.logger.warn(message, this.getMergedContext(messageContext))
34
+ }
35
+
36
+ private getMergedContext = (messageContext?: Record<string, any>) => {
37
+ return {
38
+ ...this.commonContext,
39
+ ...messageContext,
40
+ }
41
+ }
42
+ }
@@ -0,0 +1,3 @@
1
+ // created from 'create-ts-index'
2
+
3
+ export * from './LoggerDatadog';
@@ -0,0 +1,114 @@
1
+ export const convertNumberToTime = (num: number) => {
2
+ const hours = Math.floor(num / 60)
3
+ const minutes = num % 60
4
+ return `${hours}:${minutes.toString().padStart(2, "0")}`
5
+ }
6
+
7
+ export const getDomainWithSubdomains = (url: any) => {
8
+ let domainParts = 2
9
+ url = url.replace(/(https?:\/\/)?(www.)?/i, "")
10
+ url = url.split(".")
11
+ if (url.length > 2) {
12
+ domainParts = 3
13
+ }
14
+ url = url.slice(url.length - domainParts).join(".")
15
+ if (url.indexOf("/") !== -1) {
16
+ return url.split("/")[0]
17
+ }
18
+ return url
19
+ }
20
+
21
+ export const abbreviateNumber = (value: number) => {
22
+ // from https://stackoverflow.com/a/10601315/2302269
23
+ let newValue: any = value
24
+ if (value >= 1000) {
25
+ const suffixes = ["", "K", "M", "B", "T"]
26
+ const suffixNum = Math.floor(("" + value).length / 3)
27
+ let shortValue: any = ""
28
+ for (let precision = 2; precision >= 1; precision--) {
29
+ shortValue = parseFloat(
30
+ (suffixNum != 0
31
+ ? value / Math.pow(1000, suffixNum)
32
+ : value
33
+ ).toPrecision(precision),
34
+ )
35
+ const dotLessShortValue = (shortValue + "").replace(
36
+ /[^a-zA-Z 0-9]+/g,
37
+ "",
38
+ )
39
+ if (dotLessShortValue.length <= 2) {
40
+ break
41
+ }
42
+ }
43
+ if (shortValue % 1 != 0) shortValue = shortValue.toFixed(1)
44
+ newValue = shortValue + suffixes[suffixNum]
45
+ }
46
+ return newValue
47
+ }
48
+
49
+ export const replaceBrTagsWithNewLines = (str: string) => {
50
+ return str.replace(/<br\s*\/?>/gi, "\n")
51
+ }
52
+
53
+ export const capitalizeText = (s: string | null) => {
54
+ if (typeof s !== "string") return ""
55
+ return s.charAt(0).toUpperCase() + s.slice(1)
56
+ }
57
+
58
+ export const optionalKeyValue = (key: string, value: any) => {
59
+ return !!value ? { [key]: value } : {}
60
+ }
61
+
62
+ export const getDomainWithDotPrefix = (domain: string) => {
63
+ return `.${domain}`
64
+ }
65
+ export const getDomainWithoutDotPrefix = (
66
+ cookieDomainWithDotPrefix?: string,
67
+ ) => {
68
+ return cookieDomainWithDotPrefix
69
+ ? cookieDomainWithDotPrefix.substring(1)
70
+ : ""
71
+ }
72
+
73
+ export const isObject = (item: any) => {
74
+ return item && typeof item === "object" && !Array.isArray(item)
75
+ }
76
+
77
+ export const padZero = (str: string | number, len?: number) => {
78
+ len = len || 2
79
+ let zeros = new Array(len).join("0")
80
+ return (zeros + str).slice(-len)
81
+ }
82
+
83
+ export const getFunctionArgumentNames = (func: Function) => {
84
+ const STRIP_COMMENTS =
85
+ /(\/\/.*$)|(\/\*[\s\S]*?\*\/)|(\s*=[^,\)]*(('(?:\\'|[^'\r\n])*')|("(?:\\"|[^"\r\n])*"))|(\s*=[^,\)]*))/gm
86
+ const ARGUMENT_NAMES = /([^\s,]+)/g
87
+ const fnStr = func.toString().replace(STRIP_COMMENTS, "")
88
+ let result: any = fnStr
89
+ .slice(fnStr.indexOf("(") + 1, fnStr.indexOf(")"))
90
+ .match(ARGUMENT_NAMES)
91
+ if (result === null) result = []
92
+ return result
93
+ }
94
+
95
+ export const removeByIndex = (array: any[], index: number | number[]) => {
96
+ return array.filter((el, i) => {
97
+ return Array.isArray(index) ? !index.includes(i) : index !== i
98
+ })
99
+ }
100
+
101
+ export const getRandomIdOfNCharacters = (length: number) => {
102
+ let result = ""
103
+ const characters =
104
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
105
+ const charactersLength = characters.length
106
+ let counter = 0
107
+ while (counter < length) {
108
+ result += characters.charAt(
109
+ Math.floor(Math.random() * charactersLength),
110
+ )
111
+ counter += 1
112
+ }
113
+ return result
114
+ }
@@ -0,0 +1,161 @@
1
+ import isPromise from "is-promise"
2
+
3
+ function hasSymbols() {
4
+ if (
5
+ typeof Symbol !== "function" ||
6
+ typeof Object.getOwnPropertySymbols !== "function"
7
+ ) {
8
+ return false
9
+ }
10
+ if (typeof Symbol.iterator === "symbol") {
11
+ return true
12
+ }
13
+
14
+ var obj: any = {}
15
+ var sym = Symbol("test")
16
+ var symObj = Object(sym)
17
+ if (typeof sym === "string") {
18
+ return false
19
+ }
20
+
21
+ if (Object.prototype.toString.call(sym) !== "[object Symbol]") {
22
+ return false
23
+ }
24
+ if (Object.prototype.toString.call(symObj) !== "[object Symbol]") {
25
+ return false
26
+ }
27
+
28
+ // temp disabled per https://github.com/ljharb/object.assign/issues/17
29
+ // if (sym instanceof Symbol) { return false; }
30
+ // temp disabled per https://github.com/WebReflection/get-own-property-symbols/issues/4
31
+ // if (!(symObj instanceof Symbol)) { return false; }
32
+
33
+ // if (typeof Symbol.prototype.toString !== 'function') { return false; }
34
+ // if (String(sym) !== Symbol.prototype.toString.call(sym)) { return false; }
35
+
36
+ var symVal = 42
37
+ obj[sym] = symVal
38
+ for (const sym in obj) {
39
+ return false
40
+ } // eslint-disable-line no-restricted-syntax, no-unreachable-loop
41
+ if (typeof Object.keys === "function" && Object.keys(obj).length !== 0) {
42
+ return false
43
+ }
44
+
45
+ if (
46
+ typeof Object.getOwnPropertyNames === "function" &&
47
+ Object.getOwnPropertyNames(obj).length !== 0
48
+ ) {
49
+ return false
50
+ }
51
+
52
+ var syms = Object.getOwnPropertySymbols(obj)
53
+ if (syms.length !== 1 || syms[0] !== sym) {
54
+ return false
55
+ }
56
+
57
+ if (!Object.prototype.propertyIsEnumerable.call(obj, sym)) {
58
+ return false
59
+ }
60
+
61
+ if (typeof Object.getOwnPropertyDescriptor === "function") {
62
+ var descriptor: any = Object.getOwnPropertyDescriptor(obj, sym)
63
+ if (descriptor.value !== symVal || descriptor.enumerable !== true) {
64
+ return false
65
+ }
66
+ }
67
+
68
+ return true
69
+ }
70
+
71
+ function hasToStringTagShams() {
72
+ return hasSymbols() && !!Symbol.toStringTag
73
+ }
74
+
75
+ const toStr = Object.prototype.toString
76
+ const fnToStr = Function.prototype.toString
77
+ const isFnRegex = /^\s*async(?:\s+function(?:\s+|\()|\s*\()/
78
+ const hasToStringTag = hasToStringTagShams()
79
+ const getProto = Object.getPrototypeOf
80
+ const getAsyncFunc = function () {
81
+ // eslint-disable-line consistent-return
82
+ if (!hasToStringTag) {
83
+ return false
84
+ }
85
+ try {
86
+ return Function("return async function () {}")()
87
+ } catch (e) {}
88
+ }
89
+
90
+ export const isAsyncFunction = (fn: Function) => {
91
+ let AsyncFunction: any
92
+ if (typeof fn !== "function") {
93
+ return false
94
+ }
95
+ if (isFnRegex.test(fnToStr.call(fn))) {
96
+ return true
97
+ }
98
+ if (!hasToStringTag) {
99
+ const str = toStr.call(fn)
100
+ return str === "[object AsyncFunction]"
101
+ }
102
+ if (!getProto) {
103
+ return false
104
+ }
105
+ if (typeof AsyncFunction === "undefined") {
106
+ const asyncFunc = getAsyncFunc()
107
+ AsyncFunction = asyncFunc ? getProto(asyncFunc) : false
108
+ }
109
+ return getProto(fn) === AsyncFunction
110
+ }
111
+
112
+ const isFnRegexNormal = /^\s*(?:function)?\*/
113
+ const getGeneratorFunc = function () {
114
+ // eslint-disable-line consistent-return
115
+ if (!hasToStringTag) {
116
+ return false
117
+ }
118
+ try {
119
+ return Function("return function*() {}")()
120
+ } catch (e) {}
121
+ }
122
+
123
+ const isGeneratorFunction = (fn: any) => {
124
+ let GeneratorFunction: any
125
+ if (typeof fn !== "function") {
126
+ return false
127
+ }
128
+ if (isFnRegexNormal.test(fnToStr.call(fn))) {
129
+ return true
130
+ }
131
+ if (!hasToStringTag) {
132
+ var str = toStr.call(fn)
133
+ return str === "[object GeneratorFunction]"
134
+ }
135
+ if (!getProto) {
136
+ return false
137
+ }
138
+ if (typeof GeneratorFunction === "undefined") {
139
+ var generatorFunc = getGeneratorFunc()
140
+ GeneratorFunction = generatorFunc ? getProto(generatorFunc) : false
141
+ }
142
+ return getProto(fn) === GeneratorFunction
143
+ }
144
+
145
+ export const isPromiseOrAsyncFunction = (fn: Function) => {
146
+ const stringFn = fn.toString()
147
+
148
+ const isJestFn = (fn as any)._isMockFunction === true
149
+ const isJestFnAsync = isJestFn && (fn as any).getMockName() === "async"
150
+ // it is required while defining jest.fn that .mockName("async") is specified
151
+ // so this FN can recognize that it's async
152
+
153
+ return (
154
+ isPromise(fn) ||
155
+ isAsyncFunction(fn) ||
156
+ isGeneratorFunction(fn) ||
157
+ stringFn.includes("this,null,function*") ||
158
+ (stringFn.includes("__async") && stringFn.includes("function* ()")) ||
159
+ (isJestFn && isJestFnAsync)
160
+ )
161
+ }