@zohodesk/utils 1.0.0-temp-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/README.md +42 -0
  2. package/es/ErrorBoundary.js +29 -0
  3. package/es/StringUtils.js +290 -0
  4. package/es/abbreviateNumber.js +44 -0
  5. package/es/accessKeyCodes.js +18 -0
  6. package/es/applyOpenToCustomize.js +29 -0
  7. package/es/combineCustomStyle.js +39 -0
  8. package/es/compileClassNames.js +29 -0
  9. package/es/constantProps/constant.js +2 -0
  10. package/es/constantProps/index.js +1 -0
  11. package/es/constantProps/uiProps.js +50 -0
  12. package/es/contextFactory.js +15 -0
  13. package/es/createTemplateLiteral.js +77 -0
  14. package/es/crud/ArrayManager.js +63 -0
  15. package/es/crud/ObjectManager.js +54 -0
  16. package/es/crud/arrayCRUD.js +76 -0
  17. package/es/crud/objectCRUD.js +49 -0
  18. package/es/dataTypes.js +25 -0
  19. package/es/debounce.js +12 -0
  20. package/es/deepEqual.js +26 -0
  21. package/es/disCustomStyleProps.js +45 -0
  22. package/es/getCompliedClassName.js +12 -0
  23. package/es/getCompliedCustomId.js +16 -0
  24. package/es/getCurrentTime.js +10 -0
  25. package/es/getCustomBoundingClientRect.js +11 -0
  26. package/es/index.js +11 -0
  27. package/es/isTouchDevice.js +9 -0
  28. package/es/jsonToQueryString.js +6 -0
  29. package/es/loadResource.js +56 -0
  30. package/es/mergeStyle.js +53 -0
  31. package/es/objectUtils.js +4 -0
  32. package/es/omit.js +11 -0
  33. package/es/props/automationProps.js +7 -0
  34. package/es/props/customReducerPropTypes.js +5 -0
  35. package/es/props/customStyleProps.js +9 -0
  36. package/es/props/generalAriaProps.js +6 -0
  37. package/es/props/tagAttributeProps.js +35 -0
  38. package/es/props/tagGeneralAttributeProps.js +35 -0
  39. package/es/props/uiProps.js +95 -0
  40. package/es/reducerHandler.js +18 -0
  41. package/es/renderNode.js +28 -0
  42. package/es/replaceAtFirst.js +3 -0
  43. package/es/replaceAtLast.js +3 -0
  44. package/es/uniqueNumber.js +14 -0
  45. package/package.json +35 -0
@@ -0,0 +1,63 @@
1
+ import { appendToArray, deleteFromArrayOfObjectById, deleteFromArrayByItem, modifyArrayOfObjectItemById, modifyArrayItemByItem, emptyArray } from '@zohodesk/utils/es/arrayCRUD';
2
+ import { DUMMY_ARRAY } from '../../../dotkit/src/utils/constants';
3
+
4
+ class ArrayManager {
5
+ constructor() {
6
+ let initialArray = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : DUMMY_ARRAY;
7
+ this.array = initialArray;
8
+ }
9
+
10
+ addItem(item) {
11
+ this.array = appendToArray({
12
+ array: this.array,
13
+ item
14
+ });
15
+ return this.array;
16
+ }
17
+
18
+ removeItemById(id) {
19
+ this.array = deleteFromArrayOfObjectById({
20
+ arrayOfObject: this.array,
21
+ id
22
+ });
23
+ return this.array;
24
+ }
25
+
26
+ removeItemByItem(item) {
27
+ this.array = deleteFromArrayByItem({
28
+ array: this.array,
29
+ item
30
+ });
31
+ return this.array;
32
+ }
33
+
34
+ clearItems() {
35
+ this.array = emptyArray();
36
+ return this.array;
37
+ }
38
+
39
+ updateItemById(id, newItem) {
40
+ this.array = modifyArrayOfObjectItemById({
41
+ arrayOfObject: this.array,
42
+ id,
43
+ newItem
44
+ });
45
+ return this.array;
46
+ }
47
+
48
+ updateItemByItem(currentItem, newItem) {
49
+ this.array = modifyArrayItemByItem({
50
+ array: this.array,
51
+ currentItem,
52
+ newItem
53
+ });
54
+ return this.array;
55
+ }
56
+
57
+ getArray() {
58
+ return this.array;
59
+ }
60
+
61
+ }
62
+
63
+ export default ArrayManager;
@@ -0,0 +1,54 @@
1
+ import { emptyObject, mergeObjects, updateValueByKey as _updateObjectValueByKey, removeValueByKey as _removeObjectByKey, removeValueByKeys as _removeObjectByKeys } from '@zohodesk/utils/es/objectCRUD';
2
+ import { DUMMY_OBJECT } from '../../../dotkit/src/utils/constants';
3
+
4
+ class ObjectManager {
5
+ constructor() {
6
+ let initialObject = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : DUMMY_OBJECT;
7
+ this.object = initialObject;
8
+ }
9
+
10
+ setValue(newObject) {
11
+ this.object = mergeObjects({
12
+ object: this.object,
13
+ newObject
14
+ });
15
+ return this.object;
16
+ }
17
+
18
+ clearValue() {
19
+ this.object = emptyObject();
20
+ return this.object;
21
+ }
22
+
23
+ updateValueByKey(key, value) {
24
+ this.object = _updateObjectValueByKey({
25
+ object: this.object,
26
+ key,
27
+ value
28
+ });
29
+ return this.object;
30
+ }
31
+
32
+ removeValueByKeys(keys) {
33
+ this.object = _removeObjectByKeys({
34
+ object: this.object,
35
+ keys
36
+ });
37
+ return this.object;
38
+ }
39
+
40
+ removeValueByKey(key) {
41
+ this.object = _removeObjectByKey({
42
+ object: this.object,
43
+ key
44
+ });
45
+ return this.object;
46
+ }
47
+
48
+ getValue() {
49
+ return this.object;
50
+ }
51
+
52
+ }
53
+
54
+ export default ObjectManager;
@@ -0,0 +1,76 @@
1
+ import { DUMMY_ARRAY } from "../constantProps/constant";
2
+ import { deepEqual } from "../deepEqual";
3
+ export const appendToArray = _ref => {
4
+ let {
5
+ array,
6
+ item
7
+ } = _ref;
8
+ return [...array, item];
9
+ };
10
+ export const modifyArrayOfObjectItemById = _ref2 => {
11
+ let {
12
+ arrayOfObject,
13
+ id,
14
+ newItem
15
+ } = _ref2;
16
+ let found = false;
17
+ return arrayOfObject.map(object => {
18
+ if (!found && id !== undefined && object.id === id) {
19
+ found = true;
20
+ return Array.isArray(newItem) ? [...newItem] : typeof newItem === 'object' ? { ...object,
21
+ ...newItem
22
+ } : newItem;
23
+ }
24
+
25
+ return object;
26
+ });
27
+ };
28
+ export const modifyArrayItemByItem = _ref3 => {
29
+ let {
30
+ array,
31
+ currentItem,
32
+ newItem
33
+ } = _ref3;
34
+ let found = false;
35
+ return array.map(arrayItem => {
36
+ if (!found && currentItem !== undefined && deepEqual(arrayItem, currentItem)) {
37
+ found = true;
38
+ return newItem;
39
+ }
40
+
41
+ return arrayItem;
42
+ });
43
+ };
44
+ export const deleteFromArrayOfObjectById = _ref4 => {
45
+ let {
46
+ arrayOfObject,
47
+ id
48
+ } = _ref4;
49
+ let found = false;
50
+ return arrayOfObject.filter(object => {
51
+ if (!found && id !== undefined && object.id === id) {
52
+ found = true;
53
+ return false;
54
+ }
55
+
56
+ return true;
57
+ });
58
+ };
59
+ export const deleteFromArrayByItem = _ref5 => {
60
+ let {
61
+ array,
62
+ currentItem
63
+ } = _ref5;
64
+ let found = false;
65
+ return array.filter(arrayItem => {
66
+ if (!found && currentItem !== undefined && deepEqual(arrayItem, currentItem)) {
67
+ found = true;
68
+ return false;
69
+ }
70
+
71
+ return true;
72
+ });
73
+ };
74
+ export const emptyArray = () => {
75
+ return DUMMY_ARRAY;
76
+ };
@@ -0,0 +1,49 @@
1
+ import { DUMMY_OBJECT } from "../constantProps/constant";
2
+ export const mergeObjects = _ref => {
3
+ let {
4
+ object,
5
+ newObject
6
+ } = _ref;
7
+ return { ...object,
8
+ ...newObject
9
+ };
10
+ };
11
+ export const emptyObject = () => {
12
+ return DUMMY_OBJECT;
13
+ };
14
+ export const updateObjectValueByKey = _ref2 => {
15
+ let {
16
+ object,
17
+ key,
18
+ value
19
+ } = _ref2;
20
+ return { ...object,
21
+ [key]: value
22
+ };
23
+ };
24
+ export const removeObjectByKeys = _ref3 => {
25
+ let {
26
+ object,
27
+ keys
28
+ } = _ref3;
29
+ const newObject = { ...object
30
+ };
31
+
32
+ if (Array.isArray(keys)) {
33
+ keys.forEach(key => {
34
+ delete newObject[key];
35
+ });
36
+ }
37
+
38
+ return newObject;
39
+ };
40
+ export const removeObjectByKey = _ref4 => {
41
+ let {
42
+ object,
43
+ key
44
+ } = _ref4;
45
+ const newObject = { ...object
46
+ };
47
+ delete newObject[key];
48
+ return newObject;
49
+ };
@@ -0,0 +1,25 @@
1
+ const dataTypes = {
2
+ "number": "number",
3
+ "array": "array",
4
+ "string": "string",
5
+ "object": "object",
6
+ "bigint": "bigint",
7
+ "symbol": "symbol",
8
+ "undefined": "undefined",
9
+ "function": "function",
10
+ "boolean": "boolean"
11
+ };
12
+ export const getDataType = data => {
13
+ let typeofData = typeof data;
14
+ let isArrayDataType = Array.isArray(data);
15
+
16
+ if (isArrayDataType) {
17
+ return dataTypes.array;
18
+ } else {
19
+ return dataTypes[typeofData];
20
+ }
21
+ };
22
+ export const isDataTypeOf = (data, type) => {
23
+ return getDataType(data) === type;
24
+ };
25
+ export default dataTypes;
package/es/debounce.js ADDED
@@ -0,0 +1,12 @@
1
+ export default function debounce(fn, delay) {
2
+ let handle = null;
3
+ return function () {
4
+ if (handle) {
5
+ handle = clearTimeout(handle);
6
+ }
7
+
8
+ handle = setTimeout(() => {
9
+ fn(...arguments);
10
+ }, delay);
11
+ };
12
+ }
@@ -0,0 +1,26 @@
1
+ export const deepEqual = (a, b) => {
2
+ if (a === b) {
3
+ return true;
4
+ }
5
+
6
+ if (typeof a === 'object' && typeof b === 'object' && a !== null && b !== null) {
7
+ if (Array.isArray(a) !== Array.isArray(b)) {
8
+ return false;
9
+ }
10
+
11
+ const keysA = Object.keys(a);
12
+ const keysB = Object.keys(b);
13
+
14
+ if (keysA.length !== keysB.length) {
15
+ return false;
16
+ }
17
+
18
+ return keysA.every(key => deepEqual(a[key], b[key]));
19
+ }
20
+
21
+ if (typeof a === 'function' && typeof b === 'function') {
22
+ return a.toString().replace(/\s+/g, '') === b.toString().replace(/\s+/g, '');
23
+ }
24
+
25
+ return false;
26
+ };
@@ -0,0 +1,45 @@
1
+ // import PropTypes from "prop-types";
2
+ // import mergeStyle from "./mergeStyle";
3
+ // import { Button } from "../../base/src/Button/index";
4
+ // export default (props)=>{
5
+ // let { customStyle, openCustomStyle} = props;
6
+ // return {
7
+ // customStyle,
8
+ // openDefaultToCustomize
9
+ // };
10
+ // }
11
+ // if two is same ==> do the think replace with last , append at end
12
+ // replace vs append ==> append
13
+ // append vs replace ==> replace
14
+ // DotButton.js
15
+ // DotButton.module.css
16
+ // function DotButton({ customStyle }){
17
+ // dotButdefault => customStyle : {
18
+ // base : DotButton.style.pols
19
+ // }
20
+ // props => customStyle {
21
+ // base :
22
+ // }
23
+ // return <Button customStyle={}></Button>
24
+ // }
25
+ // Button => set of compoents
26
+ // DotButton => set of comepons
27
+ // function creatButtonWithIcon({Button,Icon}){
28
+ // return (props)=>{
29
+ // customStyle={}
30
+ // return <Button customStyle={ssss}>
31
+ // <Icon></Icon>
32
+ // </Button>
33
+ // }
34
+ // }
35
+ // Button1.js
36
+ // Icon1.js
37
+ // creatButtonWithIcon({
38
+ // Button : Button1,
39
+ // Icon : Icon1
40
+ // });
41
+ // Button1custome(props){
42
+ // props.customStyle
43
+ // combineCustomStyle()
44
+ // return Button
45
+ // }
@@ -0,0 +1,12 @@
1
+ import compileClassNames from "./compileClassNames";
2
+ export default (function (customClassName) {
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
+ return compileClassNames(...props, {
10
+ [customClassName]: !!isPlainClassName
11
+ });
12
+ });
@@ -0,0 +1,16 @@
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] : [];
8
+ let returnId = customId;
9
+
10
+ if (typeof defaultCustomId === "function") {
11
+ let options = ['', customIdPrefix, customIdSuffix, ...otherCustomOption];
12
+ returnId = defaultCustomId.apply(null, options); //returnId = defaultCustomId`${customIdPrefix} ${customIdSuffix}`;
13
+ }
14
+
15
+ return returnId ? returnId.toLowerCase() : null;
16
+ });
@@ -0,0 +1,10 @@
1
+ export default function getCurrentTime() {
2
+ const now = new Date();
3
+ const hours = now.getHours();
4
+ const minutes = now.getMinutes().toString().padStart(2, '0');
5
+ const seconds = now.getSeconds().toString().padStart(2, '0');
6
+ const ampm = hours >= 12 ? 'PM' : 'AM';
7
+ const formattedHours = hours % 12 || 12; // Convert 24-hour time to 12-hour format
8
+
9
+ return `${formattedHours}:${minutes}:${seconds} ${ampm}`;
10
+ }
@@ -0,0 +1,11 @@
1
+ export default function getCustomBoundingClientRect(element) {
2
+ const elementRect = element.getBoundingClientRect();
3
+ return {
4
+ height: elementRect.height,
5
+ width: elementRect.width,
6
+ top: elementRect.top,
7
+ left: elementRect.left,
8
+ right: document.documentElement.clientWidth - elementRect.right,
9
+ bottom: document.documentElement.clientHeight - elementRect.bottom
10
+ };
11
+ }
package/es/index.js ADDED
@@ -0,0 +1,11 @@
1
+ export { default as debounce } from './debounce';
2
+ export { default as uniqueNumber } from './uniqueNumber';
3
+ export { default as omit } from './omit';
4
+ export { default as mergeStyle } from './mergeStyle';
5
+ export { default as applyOpenToCustomize } from './applyOpenToCustomize';
6
+ export { default as combineCustomStyle } from './combineCustomStyle';
7
+ export { default as compileClassNames } from './compileClassNames';
8
+ export { default as ErrorBoundary } from './ErrorBoundary';
9
+ export { default as loadResource } from './loadResource';
10
+ export { default as accessKeyCodes, accessKeyNames } from "./accessKeyCodes";
11
+ export { default as renderNode, isRenderable } from './renderNode';
@@ -0,0 +1,9 @@
1
+ export default function isTouchDevice() {
2
+ if ('ontouchstart' in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0) {
3
+ /* browser with either Touch Events of Pointer Events
4
+ running on touch-capable device */
5
+ return true;
6
+ }
7
+
8
+ return false;
9
+ }
@@ -0,0 +1,6 @@
1
+ export default function jsonToQueryString(json) {
2
+ var str = Object.keys(json).map(function (key) {
3
+ return encodeURIComponent(key) + '=' + encodeURIComponent(json[key]);
4
+ }).join('&');
5
+ return str != '' ? '?' + str : str;
6
+ }
@@ -0,0 +1,56 @@
1
+ /* eslint-disable import/prefer-default-export */
2
+
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;
6
+ return new Promise(resolve => {
7
+ let css;
8
+ let script;
9
+
10
+ if (id && document.getElementById(id)) {
11
+ resolve();
12
+ return;
13
+ }
14
+
15
+ if (type === 'css') {
16
+ css = document.createElement('link');
17
+ css.type = 'text/css';
18
+ css.rel = 'stylesheet';
19
+ css.href = url;
20
+ css.id = id || '';
21
+
22
+ css.onload = function () {
23
+ resolve();
24
+ };
25
+
26
+ css.onerror = function () {
27
+ resolve();
28
+ };
29
+
30
+ document.getElementsByTagName('head')[0].appendChild(css);
31
+ } else if (type === 'js') {
32
+ script = document.createElement('script');
33
+ script.type = 'text/javascript';
34
+ script.src = url;
35
+ script.defer = needDefer;
36
+
37
+ script.onload = function () {
38
+ resolve();
39
+
40
+ if (id) {
41
+ script.id = id;
42
+ }
43
+
44
+ if (typeof callback === 'function') {
45
+ callback();
46
+ }
47
+ };
48
+
49
+ script.onerror = function () {
50
+ resolve();
51
+ };
52
+
53
+ document.getElementsByTagName('head')[0].appendChild(script);
54
+ }
55
+ });
56
+ }
@@ -0,0 +1,53 @@
1
+ const REPLACER_SYMBOL = '$'; // $ startWith is used for replace the existing style.
2
+ // all other will be append
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] : [];
7
+ // if(!Object.keys(defaultStyle).includes('base')) {
8
+ // throw new Error(`STYLE CUSTOMIZATION RULE - Your style sheet should have "base" class name for customization`);
9
+ // }
10
+ return Object.keys(customStyle).reduce((res, next) => {
11
+ if (next.startsWith(REPLACER_SYMBOL)) {
12
+ let styleValue = customStyle[next];
13
+ let styleName = next.replace(REPLACER_SYMBOL, '');
14
+
15
+ if (!res[styleName] && !additionalStyle.includes(styleName)) {
16
+ throw new Error(`UNKNOWN CLASSNAME DETECTED - Given customStyle's key "${styleName}" is not available in that component style`);
17
+ } else if (additionalStyle.includes(styleName)) {
18
+ res[styleName] = styleValue;
19
+ } else {
20
+ let val = res[styleName];
21
+ Object.keys(res).map(keyName => {
22
+ let styleKey = res[keyName];
23
+ let ind = styleKey.indexOf(val);
24
+
25
+ if (ind !== -1) {
26
+ res[keyName] = styleKey.replace(val, styleValue);
27
+ }
28
+ });
29
+ }
30
+ } else if (res[next]) {
31
+ let val = res[next];
32
+ Object.keys(res).map(keyName => {
33
+ let styleKey = res[keyName];
34
+ let styleClasses = styleKey.split(' ');
35
+ val = val.split(' ')[0];
36
+ let ind = styleClasses.indexOf(val);
37
+
38
+ if (ind !== -1) {
39
+ styleClasses[ind] = styleClasses[ind] + ' ' + customStyle[next];
40
+ res[keyName] = styleClasses.join(' ');
41
+ }
42
+ });
43
+ } else if (additionalStyle.includes(next)) {
44
+ res[next] = customStyle[next];
45
+ } else if (!res[next] && !additionalStyle.includes(next)) {
46
+ // res[next] = customStyle[next];
47
+ throw new Error(`UNKNOWN CLASSNAME DETECTED - Given customStyle's key "${next}" is not available in that component style`);
48
+ }
49
+
50
+ return res;
51
+ }, { ...defaultStyle
52
+ });
53
+ }
@@ -0,0 +1,4 @@
1
+ export const hasOwnProperty = Object.hasOwnProperty;
2
+ export const isToStringReferenceEqual = data => {
3
+ return data.toString === Object.prototype.toString;
4
+ };
package/es/omit.js ADDED
@@ -0,0 +1,11 @@
1
+ export default (keys => {
2
+ return function omit(obj) {
3
+ return Object.keys(obj).reduce((res, next) => {
4
+ if (!keys.includes(next)) {
5
+ res[next] = obj[next];
6
+ }
7
+
8
+ return res;
9
+ }, {});
10
+ };
11
+ });
@@ -0,0 +1,7 @@
1
+ import PropTypes from 'prop-types';
2
+ export const automationPropsName = 'customId';
3
+ export default {
4
+ customId: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
5
+ customIdPrefix: PropTypes.string,
6
+ customIdSuffix: PropTypes.string
7
+ };
@@ -0,0 +1,5 @@
1
+ import PropTypes from 'prop-types';
2
+ export default {
3
+ customReducer: PropTypes.func
4
+ };
5
+ export const customReducer = 'customReducer';
@@ -0,0 +1,9 @@
1
+ import PropTypes from 'prop-types';
2
+ export default {
3
+ customStyle: PropTypes.object
4
+ };
5
+ /**
6
+ * Prop Name
7
+ */
8
+
9
+ export const customStyle = "customStyle";
@@ -0,0 +1,6 @@
1
+ import PropTypes from 'prop-types';
2
+ export default {
3
+ 'aria-label': PropTypes.string,
4
+ 'aria-labelledby': PropTypes.string,
5
+ tabIndex: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
6
+ };
@@ -0,0 +1,35 @@
1
+ import PropTypes from 'prop-types';
2
+ /**
3
+ * tagAttributes - is used to spread these attributes for events customize and override few attributes.
4
+ */
5
+
6
+ export const tagAttributes = "tagAttributes";
7
+ const attributesList = ["tabIndex"];
8
+ const dataAttribute = "^data|on";
9
+ export const tagAttributesPropType = function (props, propName, componentName) {
10
+ if (Object.prototype.toString.call(props) !== "[object Object]") {
11
+ console.error(`Warning: Failed prop type: Invalid prop \`${propName}\` of type \`${props ? typeof props : undefined}\` supplied to \`${componentName}\`, expected \`object\`.`);
12
+ }
13
+
14
+ let curPattern = new RegExp(`^(${attributesList.join("|")})$`);
15
+ let curPattern2 = new RegExp(dataAttribute);
16
+ Object.keys(props[propName]).forEach(keyName => {
17
+ if (!(curPattern.test(keyName) || curPattern2.test(keyName))) {
18
+ console.warn(`Warning: these attributes ( ${keyName} ) aren't mentioned in ${propName}, component: ${componentName}`);
19
+ }
20
+ });
21
+ };
22
+ export const tagAttributesShape = shapeOf => (props, propName, componentName) => {
23
+ let propObject = props[propName];
24
+ propObject && Object.keys(propObject).forEach(keyName => {
25
+ if (!shapeOf.hasOwnProperty(keyName)) {
26
+ console.error(`Warning: these key ( ${keyName} ) aren't allowed in ${propName}, component: ${componentName}`);
27
+ return;
28
+ }
29
+
30
+ shapeOf[keyName](propObject, keyName, `${componentName} prop: ${propName}.${keyName} `);
31
+ });
32
+ };
33
+ export default {
34
+ [tagAttributes]: tagAttributesPropType
35
+ };
@@ -0,0 +1,35 @@
1
+ import PropTypes from 'prop-types';
2
+ /**
3
+ * tagGeneralAttributes - is to spread the global attributes in component.
4
+ */
5
+
6
+ export const tagGeneralAttributes = "tagGeneralAttributes";
7
+ const attributesList = ["accesskey", "contentEditable", "dir", "draggable", "hidden", "lang", "tabIndex", "title", "spellcheck", "translate"];
8
+ const dataAttribute = "^data|on";
9
+ export const tagGeneralAttributesPropType = function (props, propName, componentName) {
10
+ if (Object.prototype.toString.call(props) !== "[object Object]") {
11
+ console.error(`Warning: Failed prop type: Invalid prop \`${propName}\` of type \`${props ? typeof props : undefined}\` supplied to \`${componentName}\`, expected \`object\`.`);
12
+ }
13
+
14
+ let curPattern = new RegExp(`^(${attributesList.join("|")})$`);
15
+ let curPattern2 = new RegExp(dataAttribute);
16
+ Object.keys(props[propName]).forEach(keyName => {
17
+ if (!(curPattern.test(keyName) || curPattern2.test(keyName))) {
18
+ console.warn(`Warning: these attributes ( ${keyName} ) aren't mentioned in tagGeneralAttributes, component: ${componentName}`);
19
+ }
20
+ });
21
+ };
22
+ export const tagGeneralAttributesShape = shapeOf => (props, propName, componentName) => {
23
+ let propObject = props[propName];
24
+ propObject && Object.keys(propObject).forEach(keyName => {
25
+ if (!shapeOf.hasOwnProperty(keyName)) {
26
+ console.error(`Warning: these key ( ${keyName} ) aren't allowed in ${propName}, component: ${componentName}`);
27
+ return;
28
+ }
29
+
30
+ shapeOf[keyName](propObject, keyName, `${componentName} prop: ${propName}.${keyName} `);
31
+ });
32
+ };
33
+ export default {
34
+ [tagGeneralAttributes]: tagGeneralAttributesPropType
35
+ };