elem-dataset
Advanced tools
+56
| 'use strict'; | ||
| // <3 Modernizr | ||
| // https://raw.githubusercontent.com/Modernizr/Modernizr/master/feature-detects/dom/dataset.js | ||
| function useNative() { | ||
| const elem = document.createElement('div'); | ||
| elem.setAttribute('data-a-b', 'c'); | ||
| return Boolean(elem.dataset && elem.dataset.aB === 'c'); | ||
| } | ||
| function nativeDataset(element) { | ||
| return element.dataset; | ||
| } | ||
| module.exports = useNative() ? nativeDataset : element => { | ||
| const map = {}; | ||
| const attributes = element.attributes; | ||
| function getter() { | ||
| return this.value; | ||
| } | ||
| function setter(name, value) { | ||
| if (typeof value === 'undefined') { | ||
| this.removeAttribute(name); | ||
| } else { | ||
| this.setAttribute(name, value); | ||
| } | ||
| } | ||
| for (let i = 0, j = attributes.length; i < j; i++) { | ||
| const attribute = attributes[i]; | ||
| if (attribute) { | ||
| const name = attribute.name; | ||
| if (name.indexOf('data-') === 0) { | ||
| const prop = name.slice(5).replace(/-./g, u => { | ||
| return u.charAt(1).toUpperCase(); | ||
| }); | ||
| const value = attribute.value; | ||
| Object.defineProperty(map, prop, { | ||
| enumerable: true, | ||
| get: getter.bind({value: value || ''}), | ||
| set: setter.bind(element, name) | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| return map; | ||
| }; |
+2
-9
@@ -1,6 +0,2 @@ | ||
| (function (global, factory) { | ||
| typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
| typeof define === 'function' && define.amd ? define(factory) : | ||
| (global.elemDataset = factory()); | ||
| }(this, (function () { 'use strict'; | ||
| 'use strict'; | ||
@@ -21,3 +17,3 @@ // <3 Modernizr | ||
| var index = useNative() ? nativeDataset : function (element) { | ||
| module.exports = useNative() ? nativeDataset : function (element) { | ||
| var map = {}; | ||
@@ -63,4 +59,1 @@ var attributes = element.attributes; | ||
| return index; | ||
| }))); |
@@ -1,1 +0,1 @@ | ||
| (function(e,t){typeof exports==="object"&&typeof module!=="undefined"?module.exports=t():typeof define==="function"&&define.amd?define(t):e.elemDataset=t()})(this,function(){"use strict";function e(){var e=document.createElement("div");e.setAttribute("data-a-b","c");return Boolean(e.dataset&&e.dataset.aB==="c")}function t(e){return e.dataset}var n=e()?t:function(e){var t={};var n=e.attributes;function r(){return this.value}function a(e,t){if(typeof t==="undefined"){this.removeAttribute(e)}else{this.setAttribute(e,t)}}for(var i=0,u=n.length;i<u;i++){var o=n[i];if(o){var f=o.name;if(f.indexOf("data-")===0){var d=f.slice(5).replace(/-./g,function(e){return e.charAt(1).toUpperCase()});var c=o.value;Object.defineProperty(t,d,{enumerable:true,get:r.bind({value:c||""}),set:a.bind(e,f)})}}}return t};return n}); | ||
| "use strict";function useNative(){var e=document.createElement("div");e.setAttribute("data-a-b","c");return Boolean(e.dataset&&e.dataset.aB==="c")}function nativeDataset(e){return e.dataset}module.exports=useNative()?nativeDataset:function(e){var t={};var a=e.attributes;function r(){return this.value}function n(e,t){if(typeof t==="undefined"){this.removeAttribute(e)}else{this.setAttribute(e,t)}}for(var i=0,u=a.length;i<u;i++){var s=a[i];if(s){var v=s.name;if(v.indexOf("data-")===0){var o=v.slice(5).replace(/-./g,function(e){return e.charAt(1).toUpperCase()});var c=s.value;Object.defineProperty(t,o,{enumerable:true,get:r.bind({value:c||""}),set:n.bind(e,v)})}}}return t}; |
+5
-7
| { | ||
| "name": "elem-dataset", | ||
| "version": "1.1.0", | ||
| "version": "1.1.1", | ||
| "description": "Simple HTMLElement.dataset ponyfill", | ||
@@ -27,3 +27,3 @@ "license": "MIT", | ||
| "build": "npm test && npm run bundle && npm run minify", | ||
| "bundle": "node_modules/.bin/rollup -c", | ||
| "bundle": "node_modules/.bin/babel src/index.js > dist/index.js", | ||
| "minify": "node_modules/.bin/uglifyjs dist/index.js > dist/index.min.js -m", | ||
@@ -34,7 +34,5 @@ "test": "xo && ava" | ||
| "ava": "*", | ||
| "babel-preset-es2015-rollup": "^1.2.0", | ||
| "babel-cli": "^6.16.0", | ||
| "babel-preset-es2015": "^6.14.0", | ||
| "jsdom": "^9.6.0", | ||
| "rollup": "^0.36.1", | ||
| "rollup-plugin-babel": "^2.6.1", | ||
| "rollup-plugin-commonjs": "^5.0.4", | ||
| "uglify-js": "^2.7.3", | ||
@@ -45,3 +43,3 @@ "xo": "*" | ||
| "presets": [ | ||
| "es2015-rollup" | ||
| "es2015" | ||
| ] | ||
@@ -48,0 +46,0 @@ }, |
+1
-1
@@ -9,3 +9,3 @@ 'use strict'; | ||
| const dataset = require('./index.js'); | ||
| const dataset = require('./src/index.js'); | ||
@@ -12,0 +12,0 @@ const hasOwnProperty = Object.prototype.hasOwnProperty; |
-56
| 'use strict'; | ||
| // <3 Modernizr | ||
| // https://raw.githubusercontent.com/Modernizr/Modernizr/master/feature-detects/dom/dataset.js | ||
| function useNative() { | ||
| const elem = document.createElement('div'); | ||
| elem.setAttribute('data-a-b', 'c'); | ||
| return Boolean(elem.dataset && elem.dataset.aB === 'c'); | ||
| } | ||
| function nativeDataset(element) { | ||
| return element.dataset; | ||
| } | ||
| module.exports = useNative() ? nativeDataset : function (element) { | ||
| const map = {}; | ||
| const attributes = element.attributes; | ||
| function getter() { | ||
| return this.value; | ||
| } | ||
| function setter(name, value) { | ||
| if (typeof value === 'undefined') { | ||
| this.removeAttribute(name); | ||
| } else { | ||
| this.setAttribute(name, value); | ||
| } | ||
| } | ||
| for (let i = 0, j = attributes.length; i < j; i++) { | ||
| const attribute = attributes[i]; | ||
| if (attribute) { | ||
| const name = attribute.name; | ||
| if (name.indexOf('data-') === 0) { | ||
| const prop = name.slice(5).replace(/-./g, u => { | ||
| return u.charAt(1).toUpperCase(); | ||
| }); | ||
| const value = attribute.value; | ||
| Object.defineProperty(map, prop, { | ||
| enumerable: true, | ||
| get: getter.bind({value: value || ''}), | ||
| set: setter.bind(element, name) | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| return map; | ||
| }; |
| import babel from 'rollup-plugin-babel'; | ||
| import commonjs from 'rollup-plugin-commonjs'; | ||
| export default { | ||
| entry: 'index.js', | ||
| dest: 'dist/index.js', | ||
| format: 'umd', | ||
| moduleName: 'elemDataset', | ||
| plugins: [ | ||
| commonjs(), | ||
| babel() | ||
| ] | ||
| }; |
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
6
-25%7050
-9.02%10
-9.09%130
-12.75%