UNPKG

@deskpro/react-forms

Version:
48 lines (37 loc) 1.24 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = keyPath; var _isString = require('lodash/isString'); var _isString2 = _interopRequireDefault(_isString); var _isArray = require('lodash/isArray'); var _isArray2 = _interopRequireDefault(_isArray); var _invariant = require('invariant'); var _invariant2 = _interopRequireDefault(_invariant); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var IS_NUMBER = /^[0-9]+$/; /** * @copyright 2015, Prometheus Research, LLC */ function tryParseInt(v) { if (typeof v === 'string' && IS_NUMBER.exec(v)) { v = parseInt(v, 10); } return v; } function keyPath(value) { if ((0, _isArray2.default)(value)) { return value; } else if ((0, _isString2.default)(value)) { if (value.indexOf('.') !== -1) { value = value.split('.').filter(Boolean).map(tryParseInt); } else { value = [tryParseInt(value)]; } return value; } else if (typeof value === 'number') { return [value]; } else { (0, _invariant2.default)(false, 'keyPath can be either an array, a string or a number, got: %s', value); } }