@zohodesk/utils 1.3.16 → 1.3.17

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 (61) hide show
  1. package/README.md +16 -33
  2. package/es/StringUtils.js +2 -6
  3. package/es/abbreviateNumber.js +1 -4
  4. package/es/compileClassNames.js +4 -9
  5. package/es/constantProps/constant.js +2 -1
  6. package/es/createTemplateLiteral.js +2 -10
  7. package/es/crud/ArrayManager.js +1 -2
  8. package/es/crud/ObjectManager.js +1 -2
  9. package/es/crud/arrayCRUD.js +22 -27
  10. package/es/crud/objectCRUD.js +17 -21
  11. package/es/eventListenerUtils.js +12 -16
  12. package/es/getCompliedClassName.js +1 -6
  13. package/es/getCompliedCustomId.js +5 -7
  14. package/es/loadResource.js +1 -2
  15. package/es/mergeStyle.js +1 -3
  16. package/es/uniqueNumber.js +1 -2
  17. package/lib/ErrorBoundary.js +78 -0
  18. package/lib/StringUtils.js +343 -0
  19. package/lib/abbreviateNumber.js +51 -0
  20. package/lib/accessKeyCodes.js +26 -0
  21. package/lib/applyOpenToCustomize.js +41 -0
  22. package/lib/combineCustomStyle.js +52 -0
  23. package/lib/compileClassNames.js +45 -0
  24. package/lib/constantProps/constant.js +14 -0
  25. package/lib/constantProps/index.js +18 -0
  26. package/lib/constantProps/uiProps.js +71 -0
  27. package/lib/contextFactory.js +23 -0
  28. package/lib/createTemplateLiteral.js +84 -0
  29. package/lib/crud/ArrayManager.js +91 -0
  30. package/lib/crud/ObjectManager.js +81 -0
  31. package/lib/crud/arrayCRUD.js +110 -0
  32. package/lib/crud/objectCRUD.js +66 -0
  33. package/lib/dataTypes.js +42 -0
  34. package/lib/debounce.js +33 -0
  35. package/lib/deepEqual.js +39 -0
  36. package/lib/disCustomStyleProps.js +46 -0
  37. package/lib/eventListenerUtils.js +36 -0
  38. package/lib/getCompliedClassName.js +24 -0
  39. package/lib/getCompliedCustomId.js +37 -0
  40. package/lib/getCurrentTime.js +25 -0
  41. package/lib/getCustomBoundingClientRect.js +18 -0
  42. package/lib/index.js +135 -0
  43. package/lib/isTouchDevice.js +16 -0
  44. package/lib/jsonToQueryString.js +13 -0
  45. package/lib/loadResource.js +63 -0
  46. package/lib/mergeStyle.js +65 -0
  47. package/lib/objectUtils.js +14 -0
  48. package/lib/omit.js +20 -0
  49. package/lib/props/automationProps.js +19 -0
  50. package/lib/props/customReducerPropTypes.js +17 -0
  51. package/lib/props/customStyleProps.js +21 -0
  52. package/lib/props/generalAriaProps.js +17 -0
  53. package/lib/props/tagAttributeProps.js +58 -0
  54. package/lib/props/tagGeneralAttributeProps.js +58 -0
  55. package/lib/props/uiProps.js +138 -0
  56. package/lib/reducerHandler.js +42 -0
  57. package/lib/renderNode.js +44 -0
  58. package/lib/replaceAtFirst.js +12 -0
  59. package/lib/replaceAtLast.js +10 -0
  60. package/lib/uniqueNumber.js +22 -0
  61. package/package.json +28 -12
package/README.md CHANGED
@@ -1,42 +1,25 @@
1
- # DOT - Util Collections
1
+ # @zohodesk/utils
2
2
 
3
- In this package, We provide lots of util fuctions for web devlopment
3
+ Common JavaScript utilities for DOT web development. This package is part of the **DOT System** (**D**esign **O**ptimization **T**hinking).
4
4
 
5
- # 1.3.16
5
+ ## Installation
6
6
 
7
- - `getCurrentTime`, `objectCRUD`, `arrayCRUD`, `deepEqual`, `ArrayManager`, `ObjectManager`, `eventListenerUtils` util added.
8
-
9
- # 1.3.15
10
-
11
- - **renderNodeUtils**
12
- - `renderNode` function added to render content that can be either a function or a React element.
13
- - `isRenderable` function added to check if the content is renderable (either a function or a valid React element).
14
-
15
- # 1.3.14
16
-
17
- - **StringUtils**
18
- - String Utils Methods added
7
+ ```bash
8
+ npm install @zohodesk/utils
9
+ ```
19
10
 
20
- # 1.3.13
11
+ ## Usage
21
12
 
22
- - `mergeStyle` issue fix
13
+ Import the utilities you need from `@zohodesk/utils` and use them across your app for shared helpers, including:
23
14
 
24
- # 1.3.12
15
+ - Class name and style helpers
16
+ - Event and timing utilities
17
+ - Small React and DOM-related helpers
25
18
 
26
- - `mergeStyle` functionality updated. Composed classes will also update if it is inside that file.
27
19
  ```js
28
- // Before
29
- const style = mergeStyle(
30
- { a: '1', b: '1 2' },
31
- { a: 'a' }
32
- );
33
- // style = { a: '1 a', b: '1 2' }
34
-
35
- // After
36
- const style = mergeStyle(
37
- { a: '1', b: '1 2' },
38
- { a: 'a' }
39
- );
40
- // style = { a: '1 a', b: '1 a 2' }
20
+ import * as utils from '@zohodesk/utils';
41
21
  ```
42
- - `compileClassNames` code optimized. Will not support other than object as argument.
22
+
23
+ ## Changelog
24
+
25
+ See [CHANGELOG.md](https://unpkg.com/@zohodesk/utils/CHANGELOG.md) for details on release history and updates.
package/es/StringUtils.js CHANGED
@@ -138,9 +138,7 @@ export function reverseString(string) {
138
138
  * @returns {string} The truncated string.
139
139
  */
140
140
 
141
- export function truncate(string) {
142
- let maxLength = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
143
-
141
+ export function truncate(string, maxLength = 1) {
144
142
  if (!isStringEmpty() && string.length > maxLength) {
145
143
  return string.slice(0, maxLength) + '...';
146
144
  }
@@ -197,9 +195,7 @@ export function toSnakeCase(string) {
197
195
  * @returns {string} The concatenated string with the specified delimiter.
198
196
  */
199
197
 
200
- export function joinArray(array) {
201
- let delimiter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ', ';
202
-
198
+ export function joinArray(array, delimiter = ', ') {
203
199
  if (Array.isArray(array)) {
204
200
  return array.join(delimiter);
205
201
  }
@@ -7,10 +7,7 @@
7
7
  * @param {number} options.decimals
8
8
  * @returns
9
9
  */
10
- export default function abbreviateNumber() {
11
- let count = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
12
- let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
13
-
10
+ export default function abbreviateNumber(count = 0, options = {}) {
14
11
  if (typeof count === 'string') {
15
12
  count = parseInt(count);
16
13
  }
@@ -1,9 +1,9 @@
1
1
  import dataTypes, { isDataTypeOf } from "./dataTypes";
2
2
  import { hasOwnProperty } from "./objectUtils";
3
3
 
4
- const collectClassNames = function (processedClassNameList) {
5
- for (let index = 0; index < (arguments.length <= 1 ? 0 : arguments.length - 1); index++) {
6
- let classInfo = index + 1 < 1 || arguments.length <= index + 1 ? undefined : arguments[index + 1];
4
+ const collectClassNames = (processedClassNameList, ...classinfoList) => {
5
+ for (let index = 0; index < classinfoList.length; index++) {
6
+ let classInfo = classinfoList[index];
7
7
 
8
8
  if (!classInfo) {
9
9
  continue;
@@ -17,13 +17,8 @@ const collectClassNames = function (processedClassNameList) {
17
17
  }
18
18
  };
19
19
 
20
- export default function compileClassNames() {
20
+ export default function compileClassNames(...classinfoList) {
21
21
  let resultClassNameList = [];
22
-
23
- for (var _len = arguments.length, classinfoList = new Array(_len), _key = 0; _key < _len; _key++) {
24
- classinfoList[_key] = arguments[_key];
25
- }
26
-
27
22
  collectClassNames(resultClassNameList, ...classinfoList);
28
23
  return resultClassNameList.filter(a => a !== '').join(' ');
29
24
  }
@@ -1,2 +1,3 @@
1
1
  export const DUMMY_OBJECT = Object.freeze({});
2
- export const DUMMY_ARRAY = Object.freeze([]);
2
+ export const DUMMY_ARRAY = Object.freeze([]);
3
+ export const DUMMY_FUNCTION = () => {};
@@ -1,13 +1,5 @@
1
- export default function createTemplateLiteral(templateStrings) {
2
- for (var _len = arguments.length, expressionKeys = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
3
- expressionKeys[_key - 1] = arguments[_key];
4
- }
5
-
6
- return function () {
7
- for (var _len2 = arguments.length, expressionValues = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
8
- expressionValues[_key2] = arguments[_key2];
9
- }
10
-
1
+ export default function createTemplateLiteral(templateStrings, ...expressionKeys) {
2
+ return (...expressionValues) => {
11
3
  let resultString = [templateStrings[0]];
12
4
  expressionKeys.forEach(function (expressionKey, i) {
13
5
  let expressionValue = expressionValues[i];
@@ -2,8 +2,7 @@ import { appendToArray, deleteFromArrayOfObjectById, deleteFromArrayByItem, upda
2
2
  import { DUMMY_ARRAY } from "../constantProps/constant";
3
3
 
4
4
  class ArrayManager {
5
- constructor() {
6
- let initialArray = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : DUMMY_ARRAY;
5
+ constructor(initialArray = DUMMY_ARRAY) {
7
6
  this.array = initialArray;
8
7
  }
9
8
 
@@ -2,8 +2,7 @@ import { emptyObject, mergeObjects, updateObjectValueByKey, removeObjectByKey, r
2
2
  import { DUMMY_OBJECT } from "../constantProps/constant";
3
3
 
4
4
  class ObjectManager {
5
- constructor() {
6
- let initialObject = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : DUMMY_OBJECT;
5
+ constructor(initialObject = DUMMY_OBJECT) {
7
6
  this.object = initialObject;
8
7
  }
9
8
 
@@ -1,18 +1,16 @@
1
1
  import { DUMMY_ARRAY } from "../constantProps/constant";
2
2
  import { deepEqual } from "../deepEqual";
3
- export const appendToArray = _ref => {
4
- let {
5
- array,
6
- item
7
- } = _ref;
3
+ export const appendToArray = ({
4
+ array,
5
+ item
6
+ }) => {
8
7
  return [...array, item];
9
8
  };
10
- export const updateArrayOfObjectItemById = _ref2 => {
11
- let {
12
- arrayOfObject,
13
- id,
14
- newItem
15
- } = _ref2;
9
+ export const updateArrayOfObjectItemById = ({
10
+ arrayOfObject,
11
+ id,
12
+ newItem
13
+ }) => {
16
14
  let found = false;
17
15
  return arrayOfObject.map(object => {
18
16
  if (!found && id !== undefined && object.id === id) {
@@ -25,12 +23,11 @@ export const updateArrayOfObjectItemById = _ref2 => {
25
23
  return object;
26
24
  });
27
25
  };
28
- export const updateArrayItemByItem = _ref3 => {
29
- let {
30
- array,
31
- currentItem,
32
- newItem
33
- } = _ref3;
26
+ export const updateArrayItemByItem = ({
27
+ array,
28
+ currentItem,
29
+ newItem
30
+ }) => {
34
31
  let found = false;
35
32
  return array.map(arrayItem => {
36
33
  if (!found && currentItem !== undefined && deepEqual(arrayItem, currentItem)) {
@@ -41,11 +38,10 @@ export const updateArrayItemByItem = _ref3 => {
41
38
  return arrayItem;
42
39
  });
43
40
  };
44
- export const deleteFromArrayOfObjectById = _ref4 => {
45
- let {
46
- arrayOfObject,
47
- id
48
- } = _ref4;
41
+ export const deleteFromArrayOfObjectById = ({
42
+ arrayOfObject,
43
+ id
44
+ }) => {
49
45
  let found = false;
50
46
  return arrayOfObject.filter(object => {
51
47
  if (!found && id !== undefined && object.id === id) {
@@ -56,11 +52,10 @@ export const deleteFromArrayOfObjectById = _ref4 => {
56
52
  return true;
57
53
  });
58
54
  };
59
- export const deleteFromArrayByItem = _ref5 => {
60
- let {
61
- array,
62
- currentItem
63
- } = _ref5;
55
+ export const deleteFromArrayByItem = ({
56
+ array,
57
+ currentItem
58
+ }) => {
64
59
  let found = false;
65
60
  return array.filter(arrayItem => {
66
61
  if (!found && currentItem !== undefined && deepEqual(arrayItem, currentItem)) {
@@ -1,9 +1,8 @@
1
1
  import { DUMMY_OBJECT } from "../constantProps/constant";
2
- export const mergeObjects = _ref => {
3
- let {
4
- object,
5
- newObject
6
- } = _ref;
2
+ export const mergeObjects = ({
3
+ object,
4
+ newObject
5
+ }) => {
7
6
  return { ...object,
8
7
  ...newObject
9
8
  };
@@ -11,21 +10,19 @@ export const mergeObjects = _ref => {
11
10
  export const emptyObject = () => {
12
11
  return DUMMY_OBJECT;
13
12
  };
14
- export const updateObjectValueByKey = _ref2 => {
15
- let {
16
- object,
17
- key,
18
- value
19
- } = _ref2;
13
+ export const updateObjectValueByKey = ({
14
+ object,
15
+ key,
16
+ value
17
+ }) => {
20
18
  return { ...object,
21
19
  [key]: value
22
20
  };
23
21
  };
24
- export const removeObjectByKeys = _ref3 => {
25
- let {
26
- object,
27
- keys
28
- } = _ref3;
22
+ export const removeObjectByKeys = ({
23
+ object,
24
+ keys
25
+ }) => {
29
26
  const newObject = { ...object
30
27
  };
31
28
 
@@ -37,11 +34,10 @@ export const removeObjectByKeys = _ref3 => {
37
34
 
38
35
  return newObject;
39
36
  };
40
- export const removeObjectByKey = _ref4 => {
41
- let {
42
- object,
43
- key
44
- } = _ref4;
37
+ export const removeObjectByKey = ({
38
+ object,
39
+ key
40
+ }) => {
45
41
  const newObject = { ...object
46
42
  };
47
43
  delete newObject[key];
@@ -1,25 +1,21 @@
1
- export const addCustomEventListener = _ref => {
2
- let {
3
- eventName,
4
- callback,
5
- targetElement,
6
- options
7
- } = _ref;
8
-
1
+ export const addCustomEventListener = ({
2
+ eventName,
3
+ callback,
4
+ targetElement,
5
+ options
6
+ }) => {
9
7
  if (!targetElement || typeof targetElement.addEventListener !== 'function') {
10
8
  throw new Error('addCustomEventListener: targetElement is not a valid EventTarget. ' + 'Received: ' + String(targetElement));
11
9
  }
12
10
 
13
11
  targetElement.addEventListener(eventName, callback, options);
14
12
  };
15
- export const removeCustomEventListener = _ref2 => {
16
- let {
17
- eventName,
18
- callback,
19
- targetElement,
20
- options
21
- } = _ref2;
22
-
13
+ export const removeCustomEventListener = ({
14
+ eventName,
15
+ callback,
16
+ targetElement,
17
+ options
18
+ }) => {
23
19
  if (!targetElement || typeof targetElement.removeEventListener !== 'function') {
24
20
  throw new Error('removeCustomEventListener: targetElement is not a valid EventTarget. ' + 'Received: ' + String(targetElement));
25
21
  }
@@ -1,11 +1,6 @@
1
1
  import compileClassNames from "./compileClassNames";
2
- export default (function (customClassName) {
2
+ export default ((customClassName, ...props) => {
3
3
  let isPlainClassName = typeof customClassName === "string" ? true : false;
4
-
5
- for (var _len = arguments.length, props = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
6
- props[_key - 1] = arguments[_key];
7
- }
8
-
9
4
  return compileClassNames(...props, {
10
5
  [customClassName]: !!isPlainClassName
11
6
  });
@@ -1,10 +1,8 @@
1
- export default (function (_ref, defaultCustomId) {
2
- let {
3
- customId,
4
- customIdPrefix = "",
5
- customIdSuffix = ""
6
- } = _ref;
7
- let otherCustomOption = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
1
+ export default (({
2
+ customId,
3
+ customIdPrefix = "",
4
+ customIdSuffix = ""
5
+ }, defaultCustomId, otherCustomOption = []) => {
8
6
  let returnId = customId;
9
7
 
10
8
  if (typeof defaultCustomId === "function") {
@@ -1,8 +1,7 @@
1
1
  /* eslint-disable import/prefer-default-export */
2
2
 
3
3
  /* use promise and remove callback */
4
- export default function loadResource(url, type, id, callback) {
5
- let needDefer = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : true;
4
+ export default function loadResource(url, type, id, callback, needDefer = true) {
6
5
  return new Promise(resolve => {
7
6
  let css;
8
7
  let script;
package/es/mergeStyle.js CHANGED
@@ -1,9 +1,7 @@
1
1
  const REPLACER_SYMBOL = '$'; // $ startWith is used for replace the existing style.
2
2
  // all other will be append
3
3
 
4
- export default function mergeStyle(defaultStyle) {
5
- let customStyle = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
6
- let additionalStyle = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
4
+ export default function mergeStyle(defaultStyle, customStyle = {}, additionalStyle = []) {
7
5
  // if(!Object.keys(defaultStyle).includes('base')) {
8
6
  // throw new Error(`STYLE CUSTOMIZATION RULE - Your style sheet should have "base" class name for customization`);
9
7
  // }
@@ -1,5 +1,4 @@
1
- export default function uniqueNumber() {
2
- let prefix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
1
+ export default function uniqueNumber(prefix = '') {
3
2
  //let randomId = (Date.now() + Math.random()).toString(36);
4
3
  var date = Date.now(); // If created at same millisecond as previous
5
4
 
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+
3
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports["default"] = void 0;
9
+
10
+ var _react = _interopRequireDefault(require("react"));
11
+
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
13
+
14
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
15
+
16
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
17
+
18
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
19
+
20
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
21
+
22
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
23
+
24
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
25
+
26
+ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
27
+
28
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
29
+
30
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
31
+
32
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
33
+
34
+ var ErrorBoundary = /*#__PURE__*/function (_React$Component) {
35
+ _inherits(ErrorBoundary, _React$Component);
36
+
37
+ var _super = _createSuper(ErrorBoundary);
38
+
39
+ function ErrorBoundary(props) {
40
+ var _this;
41
+
42
+ _classCallCheck(this, ErrorBoundary);
43
+
44
+ _this = _super.call(this, props);
45
+ _this.state = {
46
+ hasError: false
47
+ };
48
+ return _this;
49
+ }
50
+
51
+ _createClass(ErrorBoundary, [{
52
+ key: "componentDidCatch",
53
+ value: function componentDidCatch(error, errorInfo) {
54
+ console.log(error, errorInfo);
55
+ }
56
+ }, {
57
+ key: "render",
58
+ value: function render() {
59
+ if (this.state.hasError) {
60
+ return this.props.renderFallback(this.state.error);
61
+ }
62
+
63
+ return this.props.children;
64
+ }
65
+ }], [{
66
+ key: "getDerivedStateFromError",
67
+ value: function getDerivedStateFromError(error) {
68
+ return {
69
+ hasError: true,
70
+ error: error
71
+ };
72
+ }
73
+ }]);
74
+
75
+ return ErrorBoundary;
76
+ }(_react["default"].Component);
77
+
78
+ exports["default"] = ErrorBoundary;