@commercetools-uikit/utils
Advanced tools
Comparing version 0.0.0-canary-13a4f7037f77a041964e36460e7beb59f08a246f to 0.0.0-canary-20201011131630
@@ -5,12 +5,46 @@ 'use strict'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var _Object$keys = require('@babel/runtime-corejs3/core-js-stable/object/keys'); | ||
var _filterInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/filter'); | ||
var _reduceInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/reduce'); | ||
var _Object$defineProperty = require('@babel/runtime-corejs3/core-js-stable/object/define-property'); | ||
var _Object$defineProperties = require('@babel/runtime-corejs3/core-js-stable/object/define-properties'); | ||
var _Object$getOwnPropertyDescriptors = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors'); | ||
var _forEachInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/for-each'); | ||
var _Object$getOwnPropertyDescriptor = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor'); | ||
var _Object$getOwnPropertySymbols = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols'); | ||
var _defineProperty = require('@babel/runtime-corejs3/helpers/defineProperty'); | ||
var isPropValid = require('@emotion/is-prop-valid'); | ||
var _concatInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/concat'); | ||
var warning = require('warning'); | ||
var _slicedToArray = require('@babel/runtime-corejs3/helpers/slicedToArray'); | ||
var _trimInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/trim'); | ||
var _Object$entries = require('@babel/runtime-corejs3/core-js-stable/object/entries'); | ||
var isPropValid = _interopDefault(require('@emotion/is-prop-valid')); | ||
var warning = _interopDefault(require('warning')); | ||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
var _Object$keys__default = /*#__PURE__*/_interopDefaultLegacy(_Object$keys); | ||
var _filterInstanceProperty__default = /*#__PURE__*/_interopDefaultLegacy(_filterInstanceProperty); | ||
var _reduceInstanceProperty__default = /*#__PURE__*/_interopDefaultLegacy(_reduceInstanceProperty); | ||
var _Object$defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_Object$defineProperty); | ||
var _Object$defineProperties__default = /*#__PURE__*/_interopDefaultLegacy(_Object$defineProperties); | ||
var _Object$getOwnPropertyDescriptors__default = /*#__PURE__*/_interopDefaultLegacy(_Object$getOwnPropertyDescriptors); | ||
var _forEachInstanceProperty__default = /*#__PURE__*/_interopDefaultLegacy(_forEachInstanceProperty); | ||
var _Object$getOwnPropertyDescriptor__default = /*#__PURE__*/_interopDefaultLegacy(_Object$getOwnPropertyDescriptor); | ||
var _Object$getOwnPropertySymbols__default = /*#__PURE__*/_interopDefaultLegacy(_Object$getOwnPropertySymbols); | ||
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty); | ||
var isPropValid__default = /*#__PURE__*/_interopDefaultLegacy(isPropValid); | ||
var _concatInstanceProperty__default = /*#__PURE__*/_interopDefaultLegacy(_concatInstanceProperty); | ||
var warning__default = /*#__PURE__*/_interopDefaultLegacy(warning); | ||
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray); | ||
var _trimInstanceProperty__default = /*#__PURE__*/_interopDefaultLegacy(_trimInstanceProperty); | ||
var _Object$entries__default = /*#__PURE__*/_interopDefaultLegacy(_Object$entries); | ||
var regexpData = /^data-/; | ||
function filterDataAttributes(obj) { | ||
return Object.keys(obj).filter(function (p) { | ||
var _context, _context2; | ||
return _reduceInstanceProperty__default['default'](_context = _filterInstanceProperty__default['default'](_context2 = _Object$keys__default['default'](obj)).call(_context2, function (p) { | ||
return regexpData.test(p); | ||
}).reduce(function (acc, p) { | ||
})).call(_context, function (acc, p) { | ||
// eslint-disable-next-line no-param-reassign | ||
acc[p] = obj[p]; | ||
@@ -23,5 +57,8 @@ return acc; | ||
function filterAriaAttributes(obj) { | ||
return Object.keys(obj).filter(function (p) { | ||
var _context, _context2; | ||
return _reduceInstanceProperty__default['default'](_context = _filterInstanceProperty__default['default'](_context2 = _Object$keys__default['default'](obj)).call(_context2, function (p) { | ||
return regexpData$1.test(p); | ||
}).reduce(function (acc, p) { | ||
})).call(_context, function (acc, p) { | ||
// eslint-disable-next-line no-param-reassign | ||
acc[p] = obj[p]; | ||
@@ -32,20 +69,17 @@ return acc; | ||
function filterProps(obj) { | ||
return Object.keys(obj).filter(function (prop) { | ||
return isPropValid(prop); | ||
}).reduce(function (acc, prop) { | ||
acc[prop] = obj[prop]; | ||
return acc; | ||
function ownKeys(object, enumerableOnly) { var keys = _Object$keys__default['default'](object); if (_Object$getOwnPropertySymbols__default['default']) { var symbols = _Object$getOwnPropertySymbols__default['default'](object); if (enumerableOnly) symbols = _filterInstanceProperty__default['default'](symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor__default['default'](object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { var _context3; _forEachInstanceProperty__default['default'](_context3 = ownKeys(Object(source), true)).call(_context3, function (key) { _defineProperty__default['default'](target, key, source[key]); }); } else if (_Object$getOwnPropertyDescriptors__default['default']) { _Object$defineProperties__default['default'](target, _Object$getOwnPropertyDescriptors__default['default'](source)); } else { var _context4; _forEachInstanceProperty__default['default'](_context4 = ownKeys(Object(source))).call(_context4, function (key) { _Object$defineProperty__default['default'](target, key, _Object$getOwnPropertyDescriptor__default['default'](source, key)); }); } } return target; } | ||
function filterInvalidAttributes(obj) { | ||
var _context, _context2; | ||
return _reduceInstanceProperty__default['default'](_context = _filterInstanceProperty__default['default'](_context2 = _Object$keys__default['default'](obj)).call(_context2, function (prop) { | ||
return isPropValid__default['default'](prop); | ||
})).call(_context, function (acc, prop) { | ||
return _objectSpread(_objectSpread({}, acc), {}, _defineProperty__default['default']({}, prop, obj[prop])); | ||
}, {}); | ||
} | ||
var getPassThroughProps = function getPassThroughProps(props, componentProps) { | ||
return Object.keys(props).filter(function (key) { | ||
return !componentProps.includes(key); | ||
}).reduce(function (passThroughProps, key) { | ||
passThroughProps[key] = props[key]; | ||
return passThroughProps; | ||
}, {}); | ||
}; | ||
// Given a string, validates that it has the correct format | ||
// to be a number, with decimal separators and negative sign. | ||
function isNumberish(number) { | ||
@@ -56,17 +90,32 @@ return !/[^(\-?)\d,.\s]/.test(number); | ||
var warnDeprecatedComponent = (function (componentName) { | ||
var _context; | ||
var additionalMessage = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | ||
var message = "\"".concat(componentName, "\" has been deprecated and will be removed in the next major version.").concat(additionalMessage); | ||
warning(false, message); | ||
var message = _concatInstanceProperty__default['default'](_context = "\"".concat(componentName, "\" has been deprecated and will be removed in the next major version.")).call(_context, additionalMessage); | ||
warning__default['default'](false, message); | ||
}); | ||
var warnDeprecatedProp = (function (propName, componentName, additionalMessage) { | ||
var message = "\"".concat(propName, "\" property of \"").concat(componentName, "\" has been deprecated and will be removed in the next major version.").concat(additionalMessage); | ||
warning(false, message); | ||
var _context, _context2; | ||
var message = _concatInstanceProperty__default['default'](_context = _concatInstanceProperty__default['default'](_context2 = "\"".concat(propName, "\" property of \"")).call(_context2, componentName, "\" has been deprecated and will be removed in the next major version.")).call(_context, additionalMessage); | ||
warning__default['default'](false, message); | ||
}); | ||
// Whenever a field requires a unique id, this factory can be used | ||
// to create a function returning unique ids. | ||
// | ||
// Use it as | ||
// const sequentialId = createSequentialId('text-field-'); | ||
// const element = <div id={sequentialId()}>foo</div> | ||
var createSequentialId = function createSequentialId(prefix) { | ||
var id = 0; | ||
return function () { | ||
var _context; | ||
id += 1; | ||
return "".concat(prefix).concat(id); | ||
return _concatInstanceProperty__default['default'](_context = "".concat(prefix)).call(_context, id); | ||
}; | ||
@@ -79,64 +128,26 @@ }; | ||
function _slicedToArray(arr, i) { | ||
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); | ||
} | ||
// Attempts to parse a string containing a time in either 12h or 24h format, | ||
// with precision of up to three milliseconds | ||
// Valid inputs: | ||
// 13:00 | ||
// 3:00 | ||
// 3 PM | ||
// 14:5 am | ||
// 13:00:00.000 | ||
// 13:00:60 | ||
// 13:00:59.908 | ||
// Returns an object containing | ||
// { hours, minutes, seconds, milliseconds } | ||
// or null | ||
// eslint-disable-next-line import/prefer-default-export | ||
var parseTime = function parseTime(rawTime) { | ||
if (!rawTime || typeof rawTime !== 'string') return null; | ||
function _arrayWithHoles(arr) { | ||
if (Array.isArray(arr)) return arr; | ||
} | ||
var time = _trimInstanceProperty__default['default'](rawTime).call(rawTime).toLowerCase(); | ||
function _iterableToArrayLimit(arr, i) { | ||
if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; | ||
var _arr = []; | ||
var _n = true; | ||
var _d = false; | ||
var _e = undefined; | ||
var match = time.match(/^(\d{1,2})(?::(\d{1,2})(?::(\d{1,2})(?:\.(\d{1,3}))?)?)?\s*(am|pm)?$/); | ||
if (!match) return null; // As we accept eg "3 AM" there might not be a value for minutes, seconds or | ||
// milliseconds, so we default them | ||
try { | ||
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { | ||
_arr.push(_s.value); | ||
if (i && _arr.length === i) break; | ||
} | ||
} catch (err) { | ||
_d = true; | ||
_e = err; | ||
} finally { | ||
try { | ||
if (!_n && _i["return"] != null) _i["return"](); | ||
} finally { | ||
if (_d) throw _e; | ||
} | ||
} | ||
return _arr; | ||
} | ||
function _unsupportedIterableToArray(o, minLen) { | ||
if (!o) return; | ||
if (typeof o === "string") return _arrayLikeToArray(o, minLen); | ||
var n = Object.prototype.toString.call(o).slice(8, -1); | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return Array.from(o); | ||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); | ||
} | ||
function _arrayLikeToArray(arr, len) { | ||
if (len == null || len > arr.length) len = arr.length; | ||
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
function _nonIterableRest() { | ||
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
} | ||
var parseTime = function parseTime(rawTime) { | ||
if (!rawTime || typeof rawTime !== 'string') return null; | ||
var time = rawTime.trim().toLowerCase(); | ||
var match = time.match(/^(\d{1,2})(?::(\d{1,2})(?::(\d{1,2})(?:\.(\d{1,3}))?)?)?\s*(am|pm)?$/); | ||
if (!match) return null; | ||
var _match = _slicedToArray(match, 6), | ||
var _match = _slicedToArray__default['default'](match, 6), | ||
hours = _match[1], | ||
@@ -149,7 +160,13 @@ _match$ = _match[2], | ||
milliseconds = _match$3 === void 0 ? '000' : _match$3, | ||
// eslint-disable-next-line prefer-const | ||
amPm = _match[5]; | ||
minutes = Number(minutes); | ||
seconds = Number(seconds); | ||
milliseconds = Number("0.".concat(milliseconds)) * 1000; | ||
seconds = Number(seconds); // Parses the number as a fraction to ensure that .5, .05 and .005 are | ||
// parsed correctily (they are 500, 50 and 5 respectively). | ||
milliseconds = Number("0.".concat(milliseconds)) * 1000; // edge-case: allow 24:00, but nothing over it | ||
hours = Number(hours) === 24 && minutes === 0 && seconds === 0 && milliseconds === 0 ? 0 : Number(hours); | ||
if (amPm) { | ||
@@ -163,4 +180,8 @@ if (hours > 12) return null; | ||
} | ||
if (seconds > 59) return null; | ||
if (milliseconds > 999) return null; | ||
if (milliseconds > 999) return null; // 12 pm (just like 24:00) would be on the next day, so we treat it as an | ||
// invalid value to avoid edge cases like the day jumping forward | ||
// if (amPm === 'pm' && Number(hours) === 12) return null; | ||
var hourOffset = function () { | ||
@@ -172,2 +193,3 @@ if (amPm === 'am' && hours === 12) return -12; | ||
}(); | ||
return { | ||
@@ -181,2 +203,3 @@ hours: Number(hours) + hourOffset, | ||
// eslint-disable-next-line import/prefer-default-export | ||
var canUseDOM = !!(typeof window !== 'undefined' && window.document && window.document.createElement); | ||
@@ -187,6 +210,10 @@ | ||
var addStaticFields = (function (Component, fields) { | ||
return Object.entries(fields).forEach(function (_ref) { | ||
var _ref2 = _slicedToArray(_ref, 2), | ||
var _context; | ||
return _forEachInstanceProperty__default['default'](_context = _Object$entries__default['default'](fields)).call(_context, function (_ref) { | ||
var _ref2 = _slicedToArray__default['default'](_ref, 2), | ||
key = _ref2[0], | ||
value = _ref2[1]; | ||
// eslint-disable-next-line no-param-reassign | ||
Component[key] = value; | ||
@@ -196,3 +223,4 @@ }); | ||
var version = '0.0.0-canary-13a4f7037f77a041964e36460e7beb59f08a246f'; | ||
// NOTE: This string will be replaced in the `prepare` script by the `scripts/version.js` file. | ||
var version = '0.0.0-canary-20201011131630'; | ||
@@ -204,5 +232,4 @@ exports.SafeHTMLElement = SafeHTMLElement; | ||
exports.filterDataAttributes = filterDataAttributes; | ||
exports.filterInvalidAttributes = filterProps; | ||
exports.filterInvalidAttributes = filterInvalidAttributes; | ||
exports.getFieldId = getFieldId; | ||
exports.getPassThroughProps = getPassThroughProps; | ||
exports.isNumberish = isNumberish; | ||
@@ -213,1 +240,2 @@ exports.parseTime = parseTime; | ||
exports.warnDeprecatedProp = warnDeprecatedProp; | ||
//# sourceMappingURL=utils.cjs.js.map |
{ | ||
"name": "@commercetools-uikit/utils", | ||
"version": "0.0.0-canary-13a4f7037f77a041964e36460e7beb59f08a246f", | ||
"description": "", | ||
"main": "dist/utils.cjs.js", | ||
"module": "dist/utils.esm.js", | ||
"description": "A collection of utilities shared across the other packages.", | ||
"version": "0.0.0-canary-20201011131630", | ||
"bugs": "https://github.com/commercetools/ui-kit/issues", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/commercetools/ui-kit.git", | ||
"directory": "packages/utils" | ||
}, | ||
"homepage": "https://uikit.commercetools.com", | ||
"keywords": [ | ||
"javascript", | ||
"design system", | ||
"react", | ||
"uikit" | ||
], | ||
"license": "MIT", | ||
"private": false, | ||
@@ -11,17 +23,19 @@ "publishConfig": { | ||
}, | ||
"sideEffects": false, | ||
"main": "dist/utils.cjs.js", | ||
"module": "dist/utils.es.js", | ||
"files": ["dist"], | ||
"scripts": { | ||
"prepare": "../../scripts/version.js replace", | ||
"prebuild": "rimraf dist", | ||
"build": "cross-env NODE_ENV=production rollup -c ../../rollup.config.js -i ./src/index.js" | ||
"build": "yarn build:bundles", | ||
"build:bundles": "cross-env NODE_ENV=production rollup -c ../../rollup.config.js -i ./src/index.js", | ||
"build:bundles:watch": "yarn build:bundles -w" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"sideEffects": false, | ||
"dependencies": { | ||
"@babel/runtime": "7.12.5", | ||
"@babel/runtime-corejs3": "7.12.5", | ||
"@emotion/is-prop-valid": "0.8.8", | ||
"warning": "4.0.3" | ||
}, | ||
"license": "MIT", | ||
"gitHead": "5f63d3ebfe81e02c4166958386725a1bd10f79b2" | ||
} | ||
} |
# Utils | ||
## Description | ||
A collection of utilities shared across the other packages. | ||
## Usage | ||
```js | ||
import { | ||
filterDataAttributes, | ||
// ... | ||
} from '@commercetools-uikit/utils'; | ||
``` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
44573
8
353
1
15
0
39
10
4
1
15
+ Added@babel/runtime@7.12.5
+ Added@babel/runtime@7.12.5(transitive)
+ Added@babel/runtime-corejs3@7.12.5(transitive)
+ Addedcore-js-pure@3.40.0(transitive)
+ Addedregenerator-runtime@0.13.11(transitive)