@cloudcmd/create-element
Advanced tools
| 'use strict'; | ||
| const currify = require('currify'); | ||
| const query = (a) => document.querySelector(`[data-name="${a}"]`); | ||
| const setAttribute = currify((el, obj, name) => el.setAttribute(name, obj[name])); | ||
| const set = currify((el, obj, name) => el[name] = obj[name]); | ||
| const not = currify((f, a) => !f(a)); | ||
| const isCamelCase = (a) => a != a.toLowerCase(); | ||
| module.exports = (name, options = {}) => { | ||
| const { | ||
| dataName, | ||
| notAppend, | ||
| parent = document.body, | ||
| uniq = true, | ||
| ...restOptions | ||
| } = options; | ||
| const elFound = isElementPresent(dataName); | ||
| if (uniq && elFound) | ||
| return elFound; | ||
| const el = document.createElement(name); | ||
| if (dataName) | ||
| el.dataset.name = dataName; | ||
| Object.keys(restOptions) | ||
| .filter(isCamelCase) | ||
| .map(set(el, options)); | ||
| Object.keys(restOptions) | ||
| .filter(not(isCamelCase)) | ||
| .map(setAttribute(el, options)); | ||
| if (!notAppend) | ||
| parent.appendChild(el); | ||
| return el; | ||
| }; | ||
| module.exports.isElementPresent = isElementPresent; | ||
| function isElementPresent(dataName) { | ||
| if (!dataName) | ||
| return; | ||
| return query(dataName); | ||
| } | ||
+34
-39
| { | ||
| "name": "@cloudcmd/create-element", | ||
| "version": "1.1.1", | ||
| "version": "2.0.0", | ||
| "description": "create dom element", | ||
@@ -10,22 +10,17 @@ "homepage": "http://github.com/cloudcmd/create-element", | ||
| }, | ||
| "main": "legacy/create-element.js", | ||
| "main": "lib/create-element.js", | ||
| "scripts": { | ||
| "watch": "nodemon --watch lib --exec", | ||
| "watch:client": "redrun compile -- --watch", | ||
| "watch:test": "npm run watch -- npm test", | ||
| "watch:lint": "npm run watch -- 'npm run lint'", | ||
| "watch:lint:js": "npm run watch -- \"run lint:js\"", | ||
| "watch:coverage": "redrun watch -- redrun coverage", | ||
| "coverage": "nyc npm test", | ||
| "report": "nyc report --reporter=text-lcov | coveralls", | ||
| "compile": "babel -d legacy lib", | ||
| "build": "redrun clean build:*", | ||
| "build:js": "redrun compile legacy:*", | ||
| "legacy:index": "echo \"module.exports = require('./create-element');\" > legacy/index.js", | ||
| "clean": "rimraf dist legacy", | ||
| "wisdom": "npm run build", | ||
| "lint:js": "eslint lib", | ||
| "lint": "redrun lint:*", | ||
| "test": "tape 'lib/**/*.spec.js'", | ||
| "test:update": "UPDATE_FIXTURE=1 redrun test" | ||
| "watch": "madrun watch", | ||
| "watch:client": "madrun watch:client", | ||
| "watch:test": "madrun watch:test", | ||
| "watch:lint": "madrun watch:lint", | ||
| "watch:lint:js": "madrun watch:lint:js", | ||
| "watch:coverage": "madrun watch:coverage", | ||
| "coverage": "madrun coverage", | ||
| "report": "madrun report", | ||
| "compile": "madrun compile", | ||
| "lint": "madrun lint", | ||
| "fix:lint": "madrun fix:lint", | ||
| "test": "madrun test", | ||
| "test:update": "madrun test:update" | ||
| }, | ||
@@ -43,32 +38,32 @@ "keywords": [ | ||
| "devDependencies": { | ||
| "@babel/cli": "^7.0.0", | ||
| "@babel/core": "^7.0.0", | ||
| "@babel/preset-env": "^7.0.0", | ||
| "auto-globals": "^1.0.0", | ||
| "autoprefixer": "^9.1.0", | ||
| "babel-loader": "^8.0.0", | ||
| "clean-css-loader": "^1.0.1", | ||
| "coveralls": "^3.0.0", | ||
| "css-loader": "^1.0.0", | ||
| "css-modules-require-hook": "^4.2.3", | ||
| "eslint": "^5.0.0", | ||
| "eslint": "^6.4.0", | ||
| "eslint-plugin-node": "^10.0.0", | ||
| "eslint-plugin-putout": "^2.0.0", | ||
| "madrun": "^3.0.5", | ||
| "mock-require": "^3.0.2", | ||
| "nodemon": "^1.9.2", | ||
| "nyc": "^13.0.1", | ||
| "nyc": "^14.1.1", | ||
| "putout": "^6.0.0", | ||
| "redrun": "^7.0.2", | ||
| "rimraf": "^2.5.3", | ||
| "sinon": "^6.0.0", | ||
| "sinon-called-with-diff": "^2.1.3", | ||
| "style-loader": "^0.23.0", | ||
| "stylelint": "^9.1.1", | ||
| "stylelint-config-standard": "^18.0.0", | ||
| "tape": "^4.6.0", | ||
| "url-loader": "^1.0.1", | ||
| "webpack": "^4.1.0", | ||
| "rimraf": "^3.0.0", | ||
| "stylelint": "^11.0.0", | ||
| "stylelint-config-standard": "^19.0.0", | ||
| "supertape": "^1.2.3", | ||
| "webpack-cli": "^3.0.8", | ||
| "wraptile": "^2.0.0" | ||
| "wraptile": "^3.0.0" | ||
| }, | ||
| "engines": { | ||
| "node": ">=8.3" | ||
| }, | ||
| "dependencies": { | ||
| "currify": "^3.0.0", | ||
| "fullstore": "^1.0.0" | ||
| "fullstore": "^2.0.2" | ||
| }, | ||
| "publishConfig": { | ||
| "access": "public" | ||
| } | ||
| } |
| 'use strict'; | ||
| function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | ||
| function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | ||
| var currify = require('currify/legacy'); | ||
| var query = function query(a) { | ||
| return document.querySelector("[data-name=\"".concat(a, "\"]")); | ||
| }; | ||
| var setAttribute = currify(function (el, obj, name) { | ||
| return el.setAttribute(name, obj[name]); | ||
| }); | ||
| var set = currify(function (el, obj, name) { | ||
| return el[name] = obj[name]; | ||
| }); | ||
| var not = currify(function (f, a) { | ||
| return !f(a); | ||
| }); | ||
| var isCamelCase = function isCamelCase(a) { | ||
| return a != a.toLowerCase(); | ||
| }; | ||
| module.exports = function (name) { | ||
| var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
| var dataName = options.dataName, | ||
| notAppend = options.notAppend, | ||
| _options$parent = options.parent, | ||
| parent = _options$parent === void 0 ? document.body : _options$parent, | ||
| _options$uniq = options.uniq, | ||
| uniq = _options$uniq === void 0 ? true : _options$uniq, | ||
| restOptions = _objectWithoutProperties(options, ["dataName", "notAppend", "parent", "uniq"]); | ||
| var elFound = isElementPresent(dataName); | ||
| if (uniq && elFound) return elFound; | ||
| var el = document.createElement(name); | ||
| if (dataName) el.dataset.name = dataName; | ||
| Object.keys(restOptions).filter(isCamelCase).map(set(el, options)); | ||
| Object.keys(restOptions).filter(not(isCamelCase)).map(setAttribute(el, options)); | ||
| if (!notAppend) parent.appendChild(el); | ||
| return el; | ||
| }; | ||
| module.exports.isElementPresent = isElementPresent; | ||
| function isElementPresent(dataName) { | ||
| if (!dataName) return; | ||
| return query(dataName); | ||
| } |
| module.exports = require('./create-element'); |
Sorry, the diff of this file is not supported yet
19
-24%6512
-15.22%5
-16.67%37
-15.91%+ Added
- Removed
Updated