pick-html-attribute-props
Advanced tools
| 'use strict'; | ||
| // eslint-disable-next-line spaced-comment | ||
| /**! | ||
| * pick-html-attribute-props v1.2.0 | ||
| * | ||
| * Copyright (c) 2019, Brandon D. Sara (https://bsara.dev/) | ||
| * Licensed under the ISC license (https://gitlab.com/bsara/pick-html-attribute-props/blob/master/LICENSE) | ||
| */ | ||
| let GLOBAL_HTML_ATTRIBUTE_NAMES; | ||
| function getGlobalHtmlAttrNames() { | ||
| if (!GLOBAL_HTML_ATTRIBUTE_NAMES) { | ||
| GLOBAL_HTML_ATTRIBUTE_NAMES = [ | ||
| 'accessKey', | ||
| 'autoCapitalize', | ||
| 'className', | ||
| 'contentEditable', | ||
| 'contextMenu', | ||
| 'dir', | ||
| 'draggable', | ||
| 'dropzone', | ||
| 'hidden', | ||
| 'id', | ||
| 'is', | ||
| 'itemID', | ||
| 'itemProp', | ||
| 'itemRef', | ||
| 'itemScope', | ||
| 'itemType', | ||
| 'lang', | ||
| 'slot', | ||
| 'spellCheck', | ||
| 'style', | ||
| 'tabIndex', | ||
| 'title', | ||
| 'translate' | ||
| ]; | ||
| Object.freeze(GLOBAL_HTML_ATTRIBUTE_NAMES); | ||
| } | ||
| return GLOBAL_HTML_ATTRIBUTE_NAMES; | ||
| } | ||
| let INPUT_HTML_ATTRIBUTE_NAMES; | ||
| function getInputHtmlAttrNames() { | ||
| if (!INPUT_HTML_ATTRIBUTE_NAMES) { | ||
| INPUT_HTML_ATTRIBUTE_NAMES = [ | ||
| ...getGlobalHtmlAttrNames(), | ||
| 'accept', | ||
| 'autoComplete', | ||
| 'autoFocus', | ||
| 'capture', | ||
| 'defaultValue', | ||
| 'disabled', | ||
| 'form', | ||
| 'formAction', | ||
| 'formEncType', | ||
| 'formMethod', | ||
| 'formNoValidate', | ||
| 'formTarget', | ||
| 'inputMode', | ||
| 'list', | ||
| 'max', | ||
| 'maxLength', | ||
| 'min', | ||
| 'minLength', | ||
| 'multiple', | ||
| 'name', | ||
| 'pattern', | ||
| 'placeholder', | ||
| 'readOnly', | ||
| 'required', | ||
| 'selectionDirection', | ||
| 'selectionEnd', | ||
| 'selectionStart', | ||
| 'src', | ||
| 'step', | ||
| 'type', | ||
| 'value', | ||
| 'wrap' | ||
| ]; | ||
| Object.freeze(INPUT_HTML_ATTRIBUTE_NAMES); | ||
| } | ||
| return INPUT_HTML_ATTRIBUTE_NAMES; | ||
| } | ||
| let TEXTAREA_HTML_ATTRIBUTE_NAMES; | ||
| function getTextAreaHtmlAttrNames() { | ||
| if (!TEXTAREA_HTML_ATTRIBUTE_NAMES) { | ||
| TEXTAREA_HTML_ATTRIBUTE_NAMES = [ | ||
| ...getGlobalHtmlAttrNames(), | ||
| 'cols', | ||
| 'defaultValue', | ||
| 'disabled', | ||
| 'form', | ||
| 'maxLength', | ||
| 'minLength', | ||
| 'name', | ||
| 'placeholder', | ||
| 'readOnly', | ||
| 'required', | ||
| 'rows', | ||
| 'value', | ||
| 'wrap' | ||
| ]; | ||
| Object.freeze(TEXTAREA_HTML_ATTRIBUTE_NAMES); | ||
| } | ||
| return TEXTAREA_HTML_ATTRIBUTE_NAMES; | ||
| } | ||
| exports.getGlobalHtmlAttrNames = getGlobalHtmlAttrNames; | ||
| exports.getInputHtmlAttrNames = getInputHtmlAttrNames; | ||
| exports.getTextAreaHtmlAttrNames = getTextAreaHtmlAttrNames; |
+123
| 'use strict'; | ||
| Object.defineProperty(exports, '__esModule', { value: true }); | ||
| var attrNames = require('./attr-names.js'); | ||
| var propNamePredicates = require('./prop-name-predicates.js'); | ||
| // eslint-disable-next-line spaced-comment | ||
| /** | ||
| * TODO: Add description | ||
| * | ||
| * @param {?Object} obj - TODO: Add description | ||
| * | ||
| * @param {?Object} [options] | ||
| * @param {?Boolean} [options.omitEventHandlers = false] - TODO: Add description | ||
| * | ||
| * @returns {!Object} TODO: Add description | ||
| */ | ||
| function pickGlobalHtmlAttrProps(obj, options) { | ||
| return _pickAttributes(obj, attrNames.getGlobalHtmlAttrNames(), options); | ||
| } | ||
| /** | ||
| * TODO: Add description | ||
| * | ||
| * @param {?Object} obj - TODO: Add description | ||
| * | ||
| * @param {?Object} [options] | ||
| * @param {?Boolean} [options.omitEventHandlers = false] - TODO: Add description | ||
| * | ||
| * @returns {!Object} TODO: Add description | ||
| */ | ||
| function pickInputHtmlAttrProps(obj, options) { | ||
| return _pickAttributes(obj, attrNames.getInputHtmlAttrNames(), options); | ||
| } | ||
| /** | ||
| * TODO: Add description | ||
| * | ||
| * @param {?Object} obj - TODO: Add description | ||
| * | ||
| * @param {?Object} [options] | ||
| * @param {?Boolean} [options.omitEventHandlers = false] - TODO: Add description | ||
| * | ||
| * @returns {!Object} TODO: Add description | ||
| */ | ||
| function pickTextAreaHtmlAttrProps(obj, options) { | ||
| return _pickAttributes(obj, attrNames.getTextAreaHtmlAttrNames(), options); | ||
| } | ||
| // region Private Helpers | ||
| /** @private */ | ||
| function _pickAttributes(obj, propNames, options) { | ||
| if (obj == null) { | ||
| return {}; | ||
| } | ||
| return Object.assign( | ||
| {}, | ||
| _pick(obj, propNames), | ||
| _pickDataAttrsAriaAttrsAndEventHandlers(obj, options) | ||
| ); | ||
| } | ||
| /** @private */ | ||
| function _pickDataAttrsAriaAttrsAndEventHandlers(obj, { omitEventHandlers } = {}) { | ||
| return _pickBy(obj, (omitEventHandlers ? propNamePredicates.propertyIsDataOrAriaAttr : propNamePredicates.propertyIsDataOrAriaAttrOrEventHandler)); | ||
| } | ||
| /** @private */ | ||
| function _pick(obj, ...propNames) { | ||
| if (obj == null) { | ||
| return {}; | ||
| } | ||
| const ret = {}; | ||
| for (let i = 0; i < propNames.length; i++) { | ||
| const propName = propNames[i]; | ||
| if (Object.prototype.hasOwnProperty.call(obj, propName)) { | ||
| ret[propName] = obj[propName]; | ||
| } | ||
| } | ||
| return ret; | ||
| } | ||
| /** @private */ | ||
| function _pickBy(obj, predicate) { | ||
| if (obj == null) { | ||
| return {}; | ||
| } | ||
| const ret = {}; | ||
| for (const propName in obj) { | ||
| if (Object.prototype.hasOwnProperty.call(obj, propName) && predicate(propName)) { | ||
| ret[propName] = obj[propName]; | ||
| } | ||
| } | ||
| return ret; | ||
| } | ||
| // endregion | ||
| exports.pickGlobalHtmlAttrProps = pickGlobalHtmlAttrProps; | ||
| exports.pickInputHtmlAttrProps = pickInputHtmlAttrProps; | ||
| exports.pickTextAreaHtmlAttrProps = pickTextAreaHtmlAttrProps; |
| 'use strict'; | ||
| // eslint-disable-next-line spaced-comment | ||
| /**! | ||
| * pick-html-attribute-props v1.2.0 | ||
| * | ||
| * Copyright (c) 2019, Brandon D. Sara (https://bsara.dev/) | ||
| * Licensed under the ISC license (https://gitlab.com/bsara/pick-html-attribute-props/blob/master/LICENSE) | ||
| */ | ||
| // region Private Constants | ||
| const EVENT_HANDLER_REGEX = /^on[A-Z]/; | ||
| // endregion | ||
| function propertyIsDataOrAriaAttrOrEventHandler(propName) { | ||
| return ( | ||
| typeof propName === 'string' | ||
| && (propertyIsEventHandler(propName) || propertyIsDataOrAriaAttr(propName)) | ||
| ); | ||
| } | ||
| function propertyIsDataOrAriaAttr(propName) { | ||
| return ( | ||
| typeof propName === 'string' | ||
| && (propName.startsWith('data-') || propName.startsWith('aria-')) | ||
| ); | ||
| } | ||
| function propertyIsEventHandler(propName) { | ||
| return ( | ||
| typeof propName === 'string' | ||
| && propName.match(EVENT_HANDLER_REGEX) != null | ||
| ); | ||
| } | ||
| exports.propertyIsDataOrAriaAttr = propertyIsDataOrAriaAttr; | ||
| exports.propertyIsDataOrAriaAttrOrEventHandler = propertyIsDataOrAriaAttrOrEventHandler; | ||
| exports.propertyIsEventHandler = propertyIsEventHandler; |
+1
-1
| // eslint-disable-next-line spaced-comment | ||
| /**! | ||
| * pick-html-attribute-props v1.0.1 | ||
| * pick-html-attribute-props v1.2.0 | ||
| * | ||
@@ -5,0 +5,0 @@ * Copyright (c) 2019, Brandon D. Sara (https://bsara.dev/) |
+5
-1
@@ -0,1 +1,6 @@ | ||
| # 1.2.0 | ||
| - **[NEW FEATURE]** Updated package to include node and browser compatible versions of library. | ||
| # 1.0.1 | ||
@@ -6,5 +11,4 @@ | ||
| # 1.0.0 | ||
| - Initial release. |
+1
-1
| // eslint-disable-next-line spaced-comment | ||
| /**! | ||
| * pick-html-attribute-props v1.0.1 | ||
| * pick-html-attribute-props v1.2.0 | ||
| * | ||
@@ -5,0 +5,0 @@ * Copyright (c) 2019, Brandon D. Sara (https://bsara.dev/) |
+1
-1
| ISC License (ISC) | ||
| Copyright (c) 2018, Brandon D. Sara (https://bsara.pro/) | ||
| Copyright (c) 2019, Brandon D. Sara (https://bsara.dev/) | ||
@@ -5,0 +5,0 @@ Permission to use, copy, modify, and/or distribute this software for any |
+13
-6
| { | ||
| "name": "pick-html-attribute-props", | ||
| "version": "1.0.1", | ||
| "version": "1.2.0", | ||
| "description": "Pickers and other helper functions used to filter standard HTML attribute values from an object", | ||
@@ -22,9 +22,15 @@ "authors": [ | ||
| ], | ||
| "main": "index.js", | ||
| "main": "node/index.js", | ||
| "module": "index.js", | ||
| "jsnext:main": "index.js", | ||
| "files": [ | ||
| "*.js" | ||
| "index.js", | ||
| "attr-names.js", | ||
| "prop-name-predicates.js", | ||
| "node/*.js" | ||
| ], | ||
| "scripts": { | ||
| "clean": "rm -rf ./node && rm -rf ./browser", | ||
| "prebuild": "npm run clean", | ||
| "build": "npx rollup -c rollup.config.js", | ||
| "postbuild": "git add ./node/*.js && git add ./browser/*.js", | ||
| "bump": "npm --no-git-tag-version version patch", | ||
@@ -41,3 +47,3 @@ "bump:major": "npm --no-git-tag-version version major", | ||
| "hooks": { | ||
| "pre-commit": "npx lint-staged" | ||
| "pre-commit": "npx lint-staged && npm run build" | ||
| } | ||
@@ -57,4 +63,5 @@ }, | ||
| "lint-staged": "^9.4.2", | ||
| "npm-scripts-versioning": "^1.0.0-beta.8" | ||
| "npm-scripts-versioning": "^1.0.0-beta.8", | ||
| "rollup": "^1.24.0" | ||
| } | ||
| } |
| // eslint-disable-next-line spaced-comment | ||
| /**! | ||
| * pick-html-attribute-props v1.0.1 | ||
| * pick-html-attribute-props v1.2.0 | ||
| * | ||
@@ -5,0 +5,0 @@ * Copyright (c) 2019, Brandon D. Sara (https://bsara.dev/) |
+2
-2
@@ -202,3 +202,3 @@ # pick-html-attribute-props [][npm] | ||
| > NOTE: A list of attribute names returned can be found in [`attr-names.js`](https://gitlab.com/bsara/pick-html-attribute-props/blob/master/attr-names.js#L52-83). | ||
| > NOTE: A list of attribute names returned can be found in [`attr-names.js`](https://gitlab.com/bsara/pick-html-attribute-props/blob/master/attr-names.js#L52-85). | ||
@@ -218,3 +218,3 @@ **Example** | ||
| > NOTE: A list of attribute names returned can be found in [`attr-names.js`](https://gitlab.com/bsara/pick-html-attribute-props/blob/master/attr-names.js#L99-111). | ||
| > NOTE: A list of attribute names returned can be found in [`attr-names.js`](https://gitlab.com/bsara/pick-html-attribute-props/blob/master/attr-names.js#L103-115). | ||
@@ -221,0 +221,0 @@ **Example** |
27399
31.95%10
42.86%462
100%7
16.67%