@riotjs/dom-bindings
Advanced tools
Comparing version 6.0.5 to 6.0.6
@@ -1,104 +0,11 @@ | ||
/** | ||
* Convert a string from camel case to dash-case | ||
* @param {string} string - probably a component tag name | ||
* @returns {string} component name normalized | ||
*/ | ||
import { insertBefore, removeChild, replaceChild, cleanNode, moveChildren, clearChildren } from '@riotjs/util/dom'; | ||
import { PARENT_KEY_SYMBOL, IS_PURE_SYMBOL } from '@riotjs/util/constants'; | ||
import { IF, SIMPLE, EACH, TAG, SLOT } from '@riotjs/util/binding-types'; | ||
export { default as bindingTypes } from '@riotjs/util/binding-types'; | ||
import { ATTRIBUTE, EVENT, TEXT, VALUE } from '@riotjs/util/expression-types'; | ||
export { default as expressionTypes } from '@riotjs/util/expression-types'; | ||
import { defineProperty } from '@riotjs/util/objects'; | ||
import { isTemplate, isBoolean, isObject, isFunction, isNil, isSvg } from '@riotjs/util/checks'; | ||
import { memoize, evaluateAttributeExpressions, panic } from '@riotjs/util/misc'; | ||
/** | ||
* Convert a string containing dashes to camel case | ||
* @param {string} string - input string | ||
* @returns {string} my-string -> myString | ||
*/ | ||
function dashToCamelCase(string) { | ||
return string.replace(/-(\w)/g, (_, c) => c.toUpperCase()) | ||
} | ||
/** | ||
* Move all the child nodes from a source tag to another | ||
* @param {HTMLElement} source - source node | ||
* @param {HTMLElement} target - target node | ||
* @returns {undefined} it's a void method ¯\_(ツ)_/¯ | ||
*/ | ||
// Ignore this helper because it's needed only for svg tags | ||
function moveChildren(source, target) { | ||
if (source.firstChild) { | ||
target.appendChild(source.firstChild); | ||
moveChildren(source, target); | ||
} | ||
} | ||
/** | ||
* Remove the child nodes from any DOM node | ||
* @param {HTMLElement} node - target node | ||
* @returns {undefined} | ||
*/ | ||
function cleanNode(node) { | ||
clearChildren(node.childNodes); | ||
} | ||
/** | ||
* Clear multiple children in a node | ||
* @param {HTMLElement[]} children - direct children nodes | ||
* @returns {undefined} | ||
*/ | ||
function clearChildren(children) { | ||
Array.from(children).forEach(removeChild); | ||
} | ||
/** | ||
* Remove a node | ||
* @param {HTMLElement}node - node to remove | ||
* @returns {undefined} | ||
*/ | ||
const removeChild = node => node && node.parentNode && node.parentNode.removeChild(node); | ||
/** | ||
* Insert before a node | ||
* @param {HTMLElement} newNode - node to insert | ||
* @param {HTMLElement} refNode - ref child | ||
* @returns {undefined} | ||
*/ | ||
const insertBefore = (newNode, refNode) => refNode && refNode.parentNode && refNode.parentNode.insertBefore(newNode, refNode); | ||
/** | ||
* Replace a node | ||
* @param {HTMLElement} newNode - new node to add to the DOM | ||
* @param {HTMLElement} replaced - node to replace | ||
* @returns {undefined} | ||
*/ | ||
const replaceChild = (newNode, replaced) => replaced && replaced.parentNode && replaced.parentNode.replaceChild(newNode, replaced); | ||
// Riot.js constants that can be used accross more modules | ||
const IS_PURE_SYMBOL = Symbol('pure'), | ||
PARENT_KEY_SYMBOL = Symbol('parent'); | ||
const EACH = 0; | ||
const IF = 1; | ||
const SIMPLE = 2; | ||
const TAG = 3; | ||
const SLOT = 4; | ||
var bindingTypes = { | ||
EACH, | ||
IF, | ||
SIMPLE, | ||
TAG, | ||
SLOT | ||
}; | ||
const ATTRIBUTE = 0; | ||
const EVENT = 1; | ||
const TEXT = 2; | ||
const VALUE = 3; | ||
var expressionTypes = { | ||
ATTRIBUTE, | ||
EVENT, | ||
TEXT, | ||
VALUE | ||
}; | ||
const HEAD_SYMBOL = Symbol(); | ||
@@ -140,90 +47,2 @@ const TAIL_SYMBOL = Symbol(); | ||
/** | ||
* Helper function to set an immutable property | ||
* @param {Object} source - object where the new property will be set | ||
* @param {string} key - object key where the new property will be stored | ||
* @param {*} value - value of the new property | ||
* @param {Object} options - set the propery overriding the default options | ||
* @returns {Object} - the original object modified | ||
*/ | ||
function defineProperty(source, key, value, options = {}) { | ||
/* eslint-disable fp/no-mutating-methods */ | ||
Object.defineProperty(source, key, { | ||
value, | ||
enumerable: false, | ||
writable: false, | ||
configurable: true, | ||
...options | ||
}); | ||
/* eslint-enable fp/no-mutating-methods */ | ||
return source | ||
} | ||
/** | ||
* Quick type checking | ||
* @param {*} element - anything | ||
* @param {string} type - type definition | ||
* @returns {boolean} true if the type corresponds | ||
*/ | ||
function checkType(element, type) { | ||
return typeof element === type | ||
} | ||
/** | ||
* Check if an element is part of an svg | ||
* @param {HTMLElement} el - element to check | ||
* @returns {boolean} true if we are in an svg context | ||
*/ | ||
function isSvg(el) { | ||
const owner = el.ownerSVGElement; | ||
return !!owner || owner === null | ||
} | ||
/** | ||
* Check if an element is a template tag | ||
* @param {HTMLElement} el - element to check | ||
* @returns {boolean} true if it's a <template> | ||
*/ | ||
function isTemplate(el) { | ||
return el.tagName.toLowerCase() === 'template' | ||
} | ||
/** | ||
* Check that will be passed if its argument is a function | ||
* @param {*} value - value to check | ||
* @returns {boolean} - true if the value is a function | ||
*/ | ||
function isFunction(value) { | ||
return checkType(value, 'function') | ||
} | ||
/** | ||
* Check if a value is a Boolean | ||
* @param {*} value - anything | ||
* @returns {boolean} true only for the value is a boolean | ||
*/ | ||
function isBoolean(value) { | ||
return checkType(value, 'boolean') | ||
} | ||
/** | ||
* Check if a value is an Object | ||
* @param {*} value - anything | ||
* @returns {boolean} true only for the value is an object | ||
*/ | ||
function isObject(value) { | ||
return !isNil(value) && value.constructor === Object | ||
} | ||
/** | ||
* Check if a value is null or undefined | ||
* @param {*} value - anything | ||
* @returns {boolean} true only for the 'undefined' and 'null' types | ||
*/ | ||
function isNil(value) { | ||
return value === null || value === undefined | ||
} | ||
/** | ||
* ISC License | ||
@@ -662,54 +481,2 @@ * | ||
/** | ||
* Throw an error with a descriptive message | ||
* @param { string } message - error message | ||
* @returns { undefined } hoppla.. at this point the program should stop working | ||
*/ | ||
function panic(message) { | ||
throw new Error(message) | ||
} | ||
/** | ||
* Returns the memoized (cached) function. | ||
* // borrowed from https://www.30secondsofcode.org/js/s/memoize | ||
* @param {Function} fn - function to memoize | ||
* @returns {Function} memoize function | ||
*/ | ||
function memoize(fn) { | ||
const cache = new Map(); | ||
const cached = val => { | ||
return cache.has(val) ? cache.get(val) : cache.set(val, fn.call(this, val)) && cache.get(val) | ||
}; | ||
cached.cache = cache; | ||
return cached | ||
} | ||
/** | ||
* Evaluate a list of attribute expressions | ||
* @param {Array} attributes - attribute expressions generated by the riot compiler | ||
* @returns {Object} key value pairs with the result of the computation | ||
*/ | ||
function evaluateAttributeExpressions(attributes) { | ||
return attributes.reduce((acc, attribute) => { | ||
const {value, type} = attribute; | ||
switch (true) { | ||
// spread attribute | ||
case !attribute.name && type === ATTRIBUTE: | ||
return { | ||
...acc, | ||
...value | ||
} | ||
// value attribute | ||
case type === VALUE: | ||
acc.value = attribute.value; | ||
break | ||
// normal attributes | ||
default: | ||
acc[dashToCamelCase(attribute.name)] = attribute.value; | ||
} | ||
return acc | ||
}, {}) | ||
} | ||
const ElementProto = typeof Element === 'undefined' ? {} : Element.prototype; | ||
@@ -1517,2 +1284,2 @@ const isNativeHtmlProperty = memoize(name => ElementProto.hasOwnProperty(name) ); // eslint-disable-line | ||
export { bindingTypes, create$1 as createBinding, create$4 as createExpression, expressionTypes, create as template }; | ||
export { create$1 as createBinding, create$4 as createExpression, create as template }; |
{ | ||
"name": "@riotjs/dom-bindings", | ||
"version": "6.0.5", | ||
"version": "6.0.6", | ||
"description": "Riot.js DOM bindings", | ||
@@ -11,3 +11,3 @@ "main": "dist/umd.dom-bindings.js", | ||
"lint": "eslint src/ test/ rollup.config.js", | ||
"cov": "nyc report --reporter=text-lcov | coveralls", | ||
"cov": "nyc report --reporter=lcov", | ||
"cov-html": "nyc report --reporter=html", | ||
@@ -47,2 +47,4 @@ "build": "rollup -c", | ||
"devDependencies": { | ||
"@rollup/plugin-alias": "^3.1.9", | ||
"@rollup/plugin-node-resolve": "^13.3.0", | ||
"benchmark": "^2.1.4", | ||
@@ -58,5 +60,3 @@ "chai": "^4.3.6", | ||
"nyc": "^15.1.0", | ||
"rollup": "^2.78.0", | ||
"rollup-plugin-alias": "^2.2.0", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"rollup": "^2.78.1", | ||
"sinon": "^14.0.0", | ||
@@ -63,0 +63,0 @@ "sinon-chai": "^3.7.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
150908
3817