debut des details de la page. Vu que c'est le troisieme (euh quatrieme?) composant, c'etait un peu plus rapide, mais heureusement que claude est la pour repasser derriere mes erreurs prcq en solo je n'y arriverais pas du tout!

This commit is contained in:
camille
2026-03-27 17:49:26 +01:00
parent 24e85c4471
commit 43589e583e
92 changed files with 12959 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) Gianluca Guarini
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+19
View File
@@ -0,0 +1,19 @@
# @riotjs/util
Riot.js shared util scripts
[![Build Status][ci-image]][ci-url]
[![Issue Count][qlty-image]][qlty-url]
[![NPM version][npm-version-image]][npm-url]
[![NPM downloads][npm-downloads-image]][npm-url]
[![MIT License][license-image]][license-url]
[ci-image]: https://img.shields.io/github/actions/workflow/status/riot/util/test.yml?style=flat-square
[ci-url]: https://github.com/riot/util/actions
[license-image]: https://img.shields.io/badge/license-MIT-000000.svg?style=flat-square
[license-url]: LICENSE
[npm-version-image]: https://img.shields.io/npm/v/@riotjs/util.svg?style=flat-square
[npm-downloads-image]: https://img.shields.io/npm/dm/@riotjs/util.svg?style=flat-square
[npm-url]: https://npmjs.org/package/@riotjs/util
[qlty-image]: https://qlty.sh/gh/riot/projects/util/maintainability.svg
[qlty-url]: https://qlty.sh/gh/riot/projects/util
+24
View File
@@ -0,0 +1,24 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const EACH = 0;
const IF = 1;
const SIMPLE = 2;
const TAG = 3;
const SLOT = 4;
var bindingTypes = {
EACH,
IF,
SIMPLE,
TAG,
SLOT,
};
exports.EACH = EACH;
exports.IF = IF;
exports.SIMPLE = SIMPLE;
exports.SLOT = SLOT;
exports.TAG = TAG;
exports.default = bindingTypes;
+13
View File
@@ -0,0 +1,13 @@
export const EACH = 0
export const IF = 1
export const SIMPLE = 2
export const TAG = 3
export const SLOT = 4
export default {
EACH,
IF,
SIMPLE,
TAG,
SLOT,
}
+96
View File
@@ -0,0 +1,96 @@
'use strict';
var constants = require('./constants.cjs');
/**
* 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
}
/**
* Detect node js environment
* @returns {boolean} true if the runtime is node
*/
function isNode() {
return typeof globalThis.process !== 'undefined'
}
/**
* Check if an attribute is a DOM handler
* @param {string} attribute - attribute string
* @returns {boolean} true only for dom listener attribute nodes
*/
function isEventAttribute(attribute) {
return constants.EVENT_ATTRIBUTE_RE.test(attribute)
}
exports.checkType = checkType;
exports.isBoolean = isBoolean;
exports.isEventAttribute = isEventAttribute;
exports.isFunction = isFunction;
exports.isNil = isNil;
exports.isNode = isNode;
exports.isObject = isObject;
exports.isSvg = isSvg;
exports.isTemplate = isTemplate;
+84
View File
@@ -0,0 +1,84 @@
import { EVENT_ATTRIBUTE_RE } from './constants.js'
/**
* Quick type checking
* @param {*} element - anything
* @param {string} type - type definition
* @returns {boolean} true if the type corresponds
*/
export 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
*/
export 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>
*/
export 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
*/
export 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
*/
export 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
*/
export 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
*/
export function isNil(value) {
return value === null || value === undefined
}
/**
* Detect node js environment
* @returns {boolean} true if the runtime is node
*/
export function isNode() {
return typeof globalThis.process !== 'undefined'
}
/**
* Check if an attribute is a DOM handler
* @param {string} attribute - attribute string
* @returns {boolean} true only for dom listener attribute nodes
*/
export function isEventAttribute(attribute) {
return EVENT_ATTRIBUTE_RE.test(attribute)
}
+57
View File
@@ -0,0 +1,57 @@
'use strict';
// Riot.js constants that can be used across more modules
const COMPONENTS_IMPLEMENTATION_MAP = new Map(),
DOM_COMPONENT_INSTANCE_PROPERTY = Symbol('riot-component'),
PLUGINS_SET = new Set(),
IS_DIRECTIVE = 'is',
VALUE_ATTRIBUTE = 'value',
REF_ATTRIBUTE = 'ref',
EVENT_ATTRIBUTE_RE = /^on/,
MOUNT_METHOD_KEY = 'mount',
UPDATE_METHOD_KEY = 'update',
UNMOUNT_METHOD_KEY = 'unmount',
SHOULD_UPDATE_KEY = 'shouldUpdate',
ON_BEFORE_MOUNT_KEY = 'onBeforeMount',
ON_MOUNTED_KEY = 'onMounted',
ON_BEFORE_UPDATE_KEY = 'onBeforeUpdate',
ON_UPDATED_KEY = 'onUpdated',
ON_BEFORE_UNMOUNT_KEY = 'onBeforeUnmount',
ON_UNMOUNTED_KEY = 'onUnmounted',
PROPS_KEY = 'props',
STATE_KEY = 'state',
SLOTS_KEY = 'slots',
ROOT_KEY = 'root',
IS_PURE_SYMBOL = Symbol('pure'),
IS_COMPONENT_UPDATING = Symbol('is_updating'),
PARENT_KEY_SYMBOL = Symbol('parent'),
TEMPLATE_KEY_SYMBOL = Symbol('template'),
ROOT_ATTRIBUTES_KEY_SYMBOL = Symbol('root-attributes');
exports.COMPONENTS_IMPLEMENTATION_MAP = COMPONENTS_IMPLEMENTATION_MAP;
exports.DOM_COMPONENT_INSTANCE_PROPERTY = DOM_COMPONENT_INSTANCE_PROPERTY;
exports.EVENT_ATTRIBUTE_RE = EVENT_ATTRIBUTE_RE;
exports.IS_COMPONENT_UPDATING = IS_COMPONENT_UPDATING;
exports.IS_DIRECTIVE = IS_DIRECTIVE;
exports.IS_PURE_SYMBOL = IS_PURE_SYMBOL;
exports.MOUNT_METHOD_KEY = MOUNT_METHOD_KEY;
exports.ON_BEFORE_MOUNT_KEY = ON_BEFORE_MOUNT_KEY;
exports.ON_BEFORE_UNMOUNT_KEY = ON_BEFORE_UNMOUNT_KEY;
exports.ON_BEFORE_UPDATE_KEY = ON_BEFORE_UPDATE_KEY;
exports.ON_MOUNTED_KEY = ON_MOUNTED_KEY;
exports.ON_UNMOUNTED_KEY = ON_UNMOUNTED_KEY;
exports.ON_UPDATED_KEY = ON_UPDATED_KEY;
exports.PARENT_KEY_SYMBOL = PARENT_KEY_SYMBOL;
exports.PLUGINS_SET = PLUGINS_SET;
exports.PROPS_KEY = PROPS_KEY;
exports.REF_ATTRIBUTE = REF_ATTRIBUTE;
exports.ROOT_ATTRIBUTES_KEY_SYMBOL = ROOT_ATTRIBUTES_KEY_SYMBOL;
exports.ROOT_KEY = ROOT_KEY;
exports.SHOULD_UPDATE_KEY = SHOULD_UPDATE_KEY;
exports.SLOTS_KEY = SLOTS_KEY;
exports.STATE_KEY = STATE_KEY;
exports.TEMPLATE_KEY_SYMBOL = TEMPLATE_KEY_SYMBOL;
exports.UNMOUNT_METHOD_KEY = UNMOUNT_METHOD_KEY;
exports.UPDATE_METHOD_KEY = UPDATE_METHOD_KEY;
exports.VALUE_ATTRIBUTE = VALUE_ATTRIBUTE;
+28
View File
@@ -0,0 +1,28 @@
// Riot.js constants that can be used across more modules
export const COMPONENTS_IMPLEMENTATION_MAP = new Map(),
DOM_COMPONENT_INSTANCE_PROPERTY = Symbol('riot-component'),
PLUGINS_SET = new Set(),
IS_DIRECTIVE = 'is',
VALUE_ATTRIBUTE = 'value',
REF_ATTRIBUTE = 'ref',
EVENT_ATTRIBUTE_RE = /^on/,
MOUNT_METHOD_KEY = 'mount',
UPDATE_METHOD_KEY = 'update',
UNMOUNT_METHOD_KEY = 'unmount',
SHOULD_UPDATE_KEY = 'shouldUpdate',
ON_BEFORE_MOUNT_KEY = 'onBeforeMount',
ON_MOUNTED_KEY = 'onMounted',
ON_BEFORE_UPDATE_KEY = 'onBeforeUpdate',
ON_UPDATED_KEY = 'onUpdated',
ON_BEFORE_UNMOUNT_KEY = 'onBeforeUnmount',
ON_UNMOUNTED_KEY = 'onUnmounted',
PROPS_KEY = 'props',
STATE_KEY = 'state',
SLOTS_KEY = 'slots',
ROOT_KEY = 'root',
IS_PURE_SYMBOL = Symbol('pure'),
IS_COMPONENT_UPDATING = Symbol('is_updating'),
PARENT_KEY_SYMBOL = Symbol('parent'),
TEMPLATE_KEY_SYMBOL = Symbol('template'),
ROOT_ATTRIBUTES_KEY_SYMBOL = Symbol('root-attributes')
+97
View File
@@ -0,0 +1,97 @@
'use strict';
var strings = require('./strings.cjs');
/**
* Get all the element attributes as object
* @param {HTMLElement} element - DOM node we want to parse
* @returns {object} all the attributes found as a key value pairs
*/
function DOMattributesToObject(element) {
return Array.from(element.attributes).reduce((acc, attribute) => {
acc[strings.dashToCamelCase(attribute.name)] = attribute.value;
return acc
}, {})
}
/**
* 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) {
// eslint-disable-next-line fp/no-loops
while (source.firstChild) target.appendChild(source.firstChild);
}
/**
* Remove the child nodes from any DOM node
* @param {HTMLElement} node - target node
* @returns {undefined}
*/
function cleanNode(node) {
// eslint-disable-next-line fp/no-loops
while (node.firstChild) node.removeChild(node.firstChild);
}
/**
* Clear multiple children in a node
* @param {HTMLElement[]} children - direct children nodes
* @returns {undefined}
*/
function clearChildren(children) {
// eslint-disable-next-line fp/no-loops,fp/no-let
for (let i = 0; i < children.length; i++) removeChild(children[i]);
}
/**
* Remove a node
* @param {HTMLElement}node - node to remove
* @returns {undefined}
*/
const removeChild = (node) => node.remove();
/**
* Insert before a node
* @param {HTMLElement} newNode - node to insert
* @param {HTMLElement} refNode - ref child
* @returns {undefined}
*/
const insertBefore = (newNode, refNode) =>
refNode?.parentNode?.insertBefore(newNode, refNode);
/**
* Move a node into its new position. Use the moveBefore method if it's available
* @param {HTMLElement} existingNode - node to move
* @param {HTMLElement} refNode - ref child
* @returns {undefined}
*/
const moveBefore = ((hasMoveBefore) => (existingNode, refNode) =>
hasMoveBefore
? refNode?.parentNode?.moveBefore(existingNode, refNode)
: insertBefore(existingNode, refNode))(
// Rely on the new moveBefore method to move nodes if it's available https://developer.mozilla.org/en-US/docs/Web/API/Element/moveBefore
// cache the value of the check into a boolean variable
typeof Element !== 'undefined' && Element.prototype.moveBefore,
);
/**
* 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?.parentNode?.replaceChild(newNode, replaced);
exports.DOMattributesToObject = DOMattributesToObject;
exports.cleanNode = cleanNode;
exports.clearChildren = clearChildren;
exports.insertBefore = insertBefore;
exports.moveBefore = moveBefore;
exports.moveChildren = moveChildren;
exports.removeChild = removeChild;
exports.replaceChild = replaceChild;
+86
View File
@@ -0,0 +1,86 @@
import { dashToCamelCase } from './strings.js'
/**
* Get all the element attributes as object
* @param {HTMLElement} element - DOM node we want to parse
* @returns {object} all the attributes found as a key value pairs
*/
export function DOMattributesToObject(element) {
return Array.from(element.attributes).reduce((acc, attribute) => {
acc[dashToCamelCase(attribute.name)] = attribute.value
return acc
}, {})
}
/**
* 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
export function moveChildren(source, target) {
// eslint-disable-next-line fp/no-loops
while (source.firstChild) target.appendChild(source.firstChild)
}
/**
* Remove the child nodes from any DOM node
* @param {HTMLElement} node - target node
* @returns {undefined}
*/
export function cleanNode(node) {
// eslint-disable-next-line fp/no-loops
while (node.firstChild) node.removeChild(node.firstChild)
}
/**
* Clear multiple children in a node
* @param {HTMLElement[]} children - direct children nodes
* @returns {undefined}
*/
export function clearChildren(children) {
// eslint-disable-next-line fp/no-loops,fp/no-let
for (let i = 0; i < children.length; i++) removeChild(children[i])
}
/**
* Remove a node
* @param {HTMLElement}node - node to remove
* @returns {undefined}
*/
export const removeChild = (node) => node.remove()
/**
* Insert before a node
* @param {HTMLElement} newNode - node to insert
* @param {HTMLElement} refNode - ref child
* @returns {undefined}
*/
export const insertBefore = (newNode, refNode) =>
refNode?.parentNode?.insertBefore(newNode, refNode)
/**
* Move a node into its new position. Use the moveBefore method if it's available
* @param {HTMLElement} existingNode - node to move
* @param {HTMLElement} refNode - ref child
* @returns {undefined}
*/
export const moveBefore = ((hasMoveBefore) => (existingNode, refNode) =>
hasMoveBefore
? refNode?.parentNode?.moveBefore(existingNode, refNode)
: insertBefore(existingNode, refNode))(
// Rely on the new moveBefore method to move nodes if it's available https://developer.mozilla.org/en-US/docs/Web/API/Element/moveBefore
// cache the value of the check into a boolean variable
typeof Element !== 'undefined' && Element.prototype.moveBefore,
)
/**
* Replace a node
* @param {HTMLElement} newNode - new node to add to the DOM
* @param {HTMLElement} replaced - node to replace
* @returns {undefined}
*/
export const replaceChild = (newNode, replaced) =>
replaced?.parentNode?.replaceChild(newNode, replaced)
+24
View File
@@ -0,0 +1,24 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const ATTRIBUTE = 0;
const EVENT = 1;
const TEXT = 2;
const VALUE = 3;
const REF = 4;
var expressionTypes = {
ATTRIBUTE,
EVENT,
TEXT,
VALUE,
REF,
};
exports.ATTRIBUTE = ATTRIBUTE;
exports.EVENT = EVENT;
exports.REF = REF;
exports.TEXT = TEXT;
exports.VALUE = VALUE;
exports.default = expressionTypes;
+13
View File
@@ -0,0 +1,13 @@
export const ATTRIBUTE = 0
export const EVENT = 1
export const TEXT = 2
export const VALUE = 3
export const REF = 4
export default {
ATTRIBUTE,
EVENT,
TEXT,
VALUE,
REF,
}
+40
View File
@@ -0,0 +1,40 @@
'use strict';
var checks = require('./checks.cjs');
require('./constants.cjs');
// does simply nothing
function noop() {
return this
}
/**
* Autobind the methods of a source object to itself
* @param {object} source - probably a riot tag instance
* @param {Array<string>} methods - list of the methods to autobind
* @returns {object} the original object received
*/
function autobindMethods(source, methods) {
methods.forEach((method) => {
source[method] = source[method].bind(source);
});
return source
}
/**
* Call the first argument received only if it's a function otherwise return it as it is
* @param {*} source - anything
* @returns {*} anything
*/
function callOrAssign(source) {
return checks.isFunction(source)
? source.prototype && source.prototype.constructor
? new source()
: source()
: source
}
exports.autobindMethods = autobindMethods;
exports.callOrAssign = callOrAssign;
exports.noop = noop;
+33
View File
@@ -0,0 +1,33 @@
import { isFunction } from './checks.js'
// does simply nothing
export function noop() {
return this
}
/**
* Autobind the methods of a source object to itself
* @param {object} source - probably a riot tag instance
* @param {Array<string>} methods - list of the methods to autobind
* @returns {object} the original object received
*/
export function autobindMethods(source, methods) {
methods.forEach((method) => {
source[method] = source[method].bind(source)
})
return source
}
/**
* Call the first argument received only if it's a function otherwise return it as it is
* @param {*} source - anything
* @returns {*} anything
*/
export function callOrAssign(source) {
return isFunction(source)
? source.prototype && source.prototype.constructor
? new source()
: source()
: source
}
+81
View File
@@ -0,0 +1,81 @@
'use strict';
var bindingTypes = require('./binding-types.cjs');
var checks = require('./checks.cjs');
var constants = require('./constants.cjs');
var dom = require('./dom.cjs');
var expressionTypes = require('./expression-types.cjs');
var functions = require('./functions.cjs');
var misc = require('./misc.cjs');
var objects = require('./objects.cjs');
var strings = require('./strings.cjs');
exports.EACH = bindingTypes.EACH;
exports.IF = bindingTypes.IF;
exports.SIMPLE = bindingTypes.SIMPLE;
exports.SLOT = bindingTypes.SLOT;
exports.TAG = bindingTypes.TAG;
exports.checkType = checks.checkType;
exports.isBoolean = checks.isBoolean;
exports.isEventAttribute = checks.isEventAttribute;
exports.isFunction = checks.isFunction;
exports.isNil = checks.isNil;
exports.isNode = checks.isNode;
exports.isObject = checks.isObject;
exports.isSvg = checks.isSvg;
exports.isTemplate = checks.isTemplate;
exports.COMPONENTS_IMPLEMENTATION_MAP = constants.COMPONENTS_IMPLEMENTATION_MAP;
exports.DOM_COMPONENT_INSTANCE_PROPERTY = constants.DOM_COMPONENT_INSTANCE_PROPERTY;
exports.EVENT_ATTRIBUTE_RE = constants.EVENT_ATTRIBUTE_RE;
exports.IS_COMPONENT_UPDATING = constants.IS_COMPONENT_UPDATING;
exports.IS_DIRECTIVE = constants.IS_DIRECTIVE;
exports.IS_PURE_SYMBOL = constants.IS_PURE_SYMBOL;
exports.MOUNT_METHOD_KEY = constants.MOUNT_METHOD_KEY;
exports.ON_BEFORE_MOUNT_KEY = constants.ON_BEFORE_MOUNT_KEY;
exports.ON_BEFORE_UNMOUNT_KEY = constants.ON_BEFORE_UNMOUNT_KEY;
exports.ON_BEFORE_UPDATE_KEY = constants.ON_BEFORE_UPDATE_KEY;
exports.ON_MOUNTED_KEY = constants.ON_MOUNTED_KEY;
exports.ON_UNMOUNTED_KEY = constants.ON_UNMOUNTED_KEY;
exports.ON_UPDATED_KEY = constants.ON_UPDATED_KEY;
exports.PARENT_KEY_SYMBOL = constants.PARENT_KEY_SYMBOL;
exports.PLUGINS_SET = constants.PLUGINS_SET;
exports.PROPS_KEY = constants.PROPS_KEY;
exports.REF_ATTRIBUTE = constants.REF_ATTRIBUTE;
exports.ROOT_ATTRIBUTES_KEY_SYMBOL = constants.ROOT_ATTRIBUTES_KEY_SYMBOL;
exports.ROOT_KEY = constants.ROOT_KEY;
exports.SHOULD_UPDATE_KEY = constants.SHOULD_UPDATE_KEY;
exports.SLOTS_KEY = constants.SLOTS_KEY;
exports.STATE_KEY = constants.STATE_KEY;
exports.TEMPLATE_KEY_SYMBOL = constants.TEMPLATE_KEY_SYMBOL;
exports.UNMOUNT_METHOD_KEY = constants.UNMOUNT_METHOD_KEY;
exports.UPDATE_METHOD_KEY = constants.UPDATE_METHOD_KEY;
exports.VALUE_ATTRIBUTE = constants.VALUE_ATTRIBUTE;
exports.DOMattributesToObject = dom.DOMattributesToObject;
exports.cleanNode = dom.cleanNode;
exports.clearChildren = dom.clearChildren;
exports.insertBefore = dom.insertBefore;
exports.moveBefore = dom.moveBefore;
exports.moveChildren = dom.moveChildren;
exports.removeChild = dom.removeChild;
exports.replaceChild = dom.replaceChild;
exports.ATTRIBUTE = expressionTypes.ATTRIBUTE;
exports.EVENT = expressionTypes.EVENT;
exports.REF = expressionTypes.REF;
exports.TEXT = expressionTypes.TEXT;
exports.VALUE = expressionTypes.VALUE;
exports.autobindMethods = functions.autobindMethods;
exports.callOrAssign = functions.callOrAssign;
exports.noop = functions.noop;
exports.generatePropsFromAttributes = misc.generatePropsFromAttributes;
exports.memoize = misc.memoize;
exports.panic = misc.panic;
exports.cloneDeep = objects.cloneDeep;
exports.defineDefaults = objects.defineDefaults;
exports.defineProperties = objects.defineProperties;
exports.defineProperty = objects.defineProperty;
exports.filter = objects.filter;
exports.pick = objects.pick;
exports.camelToDashCase = strings.camelToDashCase;
exports.dashToCamelCase = strings.dashToCamelCase;
+9
View File
@@ -0,0 +1,9 @@
export * from './binding-types.js'
export * from './checks.js'
export * from './constants.js'
export * from './dom.js'
export * from './expression-types.js'
export * from './functions.js'
export * from './misc.js'
export * from './objects.js'
export * from './strings.js'
+68
View File
@@ -0,0 +1,68 @@
'use strict';
var expressionTypes = require('./expression-types.cjs');
var strings = require('./strings.cjs');
/**
* Throw an error with a descriptive message
* @param { string } message - error message
* @param { string } cause - optional error cause object
* @returns { undefined } hoppla... at this point the program should stop working
*/
function panic(message, cause) {
throw new Error(message, { cause })
}
/**
* 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
}
/**
* Generate key-value pairs from a list of attributes
* @param {Array} attributes - list of attributes generated by the riot compiler, each containing type, name, and evaluate function
* @param {object} scope - the scope in which the attribute values will be evaluated
* @returns {object} An object containing key-value pairs representing the computed attribute values
*/
function generatePropsFromAttributes(attributes, scope) {
return attributes.reduce((acc, { type, name, evaluate }) => {
const value = evaluate(scope);
switch (true) {
// spread attribute
case !name && type === expressionTypes.ATTRIBUTE:
return {
...acc,
...value,
}
// ref attribute
case type === expressionTypes.REF:
acc.ref = value;
break
// value attribute
case type === expressionTypes.VALUE:
acc.value = value;
break
// normal attributes
default:
acc[strings.dashToCamelCase(name)] = value;
}
return acc
}, {})
}
exports.generatePropsFromAttributes = generatePropsFromAttributes;
exports.memoize = memoize;
exports.panic = panic;
+62
View File
@@ -0,0 +1,62 @@
import { ATTRIBUTE, VALUE, REF } from './expression-types.js'
import { dashToCamelCase } from './strings.js'
/**
* Throw an error with a descriptive message
* @param { string } message - error message
* @param { string } cause - optional error cause object
* @returns { undefined } hoppla... at this point the program should stop working
*/
export function panic(message, cause) {
throw new Error(message, { cause })
}
/**
* Returns the memoized (cached) function.
* // borrowed from https://www.30secondsofcode.org/js/s/memoize
* @param {Function} fn - function to memoize
* @returns {Function} memoize function
*/
export 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
}
/**
* Generate key-value pairs from a list of attributes
* @param {Array} attributes - list of attributes generated by the riot compiler, each containing type, name, and evaluate function
* @param {object} scope - the scope in which the attribute values will be evaluated
* @returns {object} An object containing key-value pairs representing the computed attribute values
*/
export function generatePropsFromAttributes(attributes, scope) {
return attributes.reduce((acc, { type, name, evaluate }) => {
const value = evaluate(scope)
switch (true) {
// spread attribute
case !name && type === ATTRIBUTE:
return {
...acc,
...value,
}
// ref attribute
case type === REF:
acc.ref = value
break
// value attribute
case type === VALUE:
acc.value = value
break
// normal attributes
default:
acc[dashToCamelCase(name)] = value
}
return acc
}, {})
}
+95
View File
@@ -0,0 +1,95 @@
'use strict';
var checks = require('./checks.cjs');
require('./constants.cjs');
/**
* 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 property overriding the default options
* @returns {object} - the original object modified
*/
function defineProperty(source, key, value, options = {}) {
Object.defineProperty(source, key, {
value,
enumerable: false,
writable: false,
configurable: true,
...options,
});
return source
}
/**
* Define multiple properties on a target object
* @param {object} source - object where the new properties will be set
* @param {object} properties - object containing as key pair the key + value properties
* @param {object} options - set the property overriding the default options
* @returns {object} the original object modified
*/
function defineProperties(source, properties, options) {
Object.entries(properties).forEach(([key, value]) => {
defineProperty(source, key, value, options);
});
return source
}
/**
* Define default properties if they don't exist on the source object
* @param {object} source - object that will receive the default properties
* @param {object} defaults - object containing additional optional keys
* @returns {object} the original object received enhanced
*/
function defineDefaults(source, defaults) {
Object.entries(defaults).forEach(([key, value]) => {
if (!source[key]) source[key] = value;
});
return source
}
/**
* Simple clone deep function, do not use it for classes or recursive objects!
* @param {*} source - possibly an object to clone
* @returns {*} the object we wanted to clone
*/
function cloneDeep(source) {
return structuredClone(source)
}
/**
* Like Array.prototype.filter but for objects
* @param {object} source - target object
* @param {(key: unknown, value: unknown) => boolean} filter - filter function
* @returns {object} filtered source or the original source received
*/
function filter(source, filter) {
return checks.isObject(source)
? Object.fromEntries(
Object.entries(source).filter(([key, value]) => filter(key, value)),
)
: source
}
/**
* Generate a new object picking only the properties from a given array
* @param {object} source - target object
* @param {Array} keys - list of keys that we want to copy over to the new object
* @returns {object} a new object conaining only the keys that we have picked from the keys array list
*/
function pick(source, keys) {
return checks.isObject(source)
? Object.fromEntries(keys.map((key) => [key, source[key]]))
: source
}
exports.cloneDeep = cloneDeep;
exports.defineDefaults = defineDefaults;
exports.defineProperties = defineProperties;
exports.defineProperty = defineProperty;
exports.filter = filter;
exports.pick = pick;
+85
View File
@@ -0,0 +1,85 @@
import { isObject } from './checks.js'
/**
* 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 property overriding the default options
* @returns {object} - the original object modified
*/
export function defineProperty(source, key, value, options = {}) {
Object.defineProperty(source, key, {
value,
enumerable: false,
writable: false,
configurable: true,
...options,
})
return source
}
/**
* Define multiple properties on a target object
* @param {object} source - object where the new properties will be set
* @param {object} properties - object containing as key pair the key + value properties
* @param {object} options - set the property overriding the default options
* @returns {object} the original object modified
*/
export function defineProperties(source, properties, options) {
Object.entries(properties).forEach(([key, value]) => {
defineProperty(source, key, value, options)
})
return source
}
/**
* Define default properties if they don't exist on the source object
* @param {object} source - object that will receive the default properties
* @param {object} defaults - object containing additional optional keys
* @returns {object} the original object received enhanced
*/
export function defineDefaults(source, defaults) {
Object.entries(defaults).forEach(([key, value]) => {
if (!source[key]) source[key] = value
})
return source
}
/**
* Simple clone deep function, do not use it for classes or recursive objects!
* @param {*} source - possibly an object to clone
* @returns {*} the object we wanted to clone
*/
export function cloneDeep(source) {
return structuredClone(source)
}
/**
* Like Array.prototype.filter but for objects
* @param {object} source - target object
* @param {(key: unknown, value: unknown) => boolean} filter - filter function
* @returns {object} filtered source or the original source received
*/
export function filter(source, filter) {
return isObject(source)
? Object.fromEntries(
Object.entries(source).filter(([key, value]) => filter(key, value)),
)
: source
}
/**
* Generate a new object picking only the properties from a given array
* @param {object} source - target object
* @param {Array} keys - list of keys that we want to copy over to the new object
* @returns {object} a new object conaining only the keys that we have picked from the keys array list
*/
export function pick(source, keys) {
return isObject(source)
? Object.fromEntries(keys.map((key) => [key, source[key]]))
: source
}
+104
View File
@@ -0,0 +1,104 @@
{
"name": "@riotjs/util",
"version": "10.1.2",
"description": "Riot.js util functions",
"main": "index.cjs",
"module": "index.js",
"type": "module",
"scripts": {
"prepublishOnly": "npm test && npm run build",
"lint": "npx prettier -c . && eslint *.js",
"build": "find . -name '*.js' -not -name '*.spec.js' -not -name 'rollup.config.js' -maxdepth 1 | xargs rollup -c rollup.config.js",
"test": "npm run lint && mocha -r jsdom-global/register *.spec.js"
},
"exports": {
".": {
"import": "./index.js",
"require": "./index.cjs"
},
"./dom": {
"import": "./dom.js",
"require": "./dom.cjs"
},
"./functions": {
"import": "./functions.js",
"require": "./functions.cjs"
},
"./constants": {
"import": "./constants.js",
"require": "./constants.cjs"
},
"./strings": {
"import": "./strings.js",
"require": "./strings.cjs"
},
"./objects": {
"import": "./objects.js",
"require": "./objects.cjs"
},
"./checks": {
"import": "./checks.js",
"require": "./checks.cjs"
},
"./expression-types": {
"import": "./expression-types.js",
"require": "./expression-types.cjs"
},
"./binding-types": {
"import": "./binding-types.js",
"require": "./binding-types.cjs"
},
"./misc": {
"import": "./misc.js",
"require": "./misc.cjs"
}
},
"files": [
"index.js",
"dom.js",
"functions.js",
"constants.js",
"strings.js",
"objects.js",
"checks.js",
"expression-types.js",
"binding-types.js",
"misc.js",
"index.cjs",
"dom.cjs",
"functions.cjs",
"constants.cjs",
"strings.cjs",
"objects.cjs",
"checks.cjs",
"expression-types.cjs",
"binding-types.cjs",
"misc.cjs"
],
"repository": {
"type": "git",
"url": "git+https://github.com/GianlucaGuarini/@riotjs/util.git"
},
"keywords": [
"Riot.js",
"shared",
"Riot util"
],
"author": "Gianluca Guarini <gianluca.guarini@gmail.com> (https://gianlucaguarini.com)",
"license": "MIT",
"bugs": {
"url": "https://github.com/GianlucaGuarini/@riotjs/util/issues"
},
"homepage": "https://github.com/GianlucaGuarini/@riotjs/util#readme",
"devDependencies": {
"@riotjs/prettier-config": "^1.1.0",
"chai": "^6.2.1",
"eslint": "^9.39.1",
"eslint-config-riot": "^5.0.2",
"jsdom": "27.2.0",
"jsdom-global": "3.0.2",
"mocha": "^11.7.5",
"prettier": "^3.6.2",
"rollup": "^4.53.3"
}
}
+22
View File
@@ -0,0 +1,22 @@
'use strict';
/**
* Convert a string from camel case to dash-case
* @param {string} string - probably a component tag name
* @returns {string} component name normalized
*/
function camelToDashCase(string) {
return string.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()
}
/**
* 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())
}
exports.camelToDashCase = camelToDashCase;
exports.dashToCamelCase = dashToCamelCase;
+17
View File
@@ -0,0 +1,17 @@
/**
* Convert a string from camel case to dash-case
* @param {string} string - probably a component tag name
* @returns {string} component name normalized
*/
export function camelToDashCase(string) {
return string.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()
}
/**
* Convert a string containing dashes to camel case
* @param {string} string - input string
* @returns {string} my-string -> myString
*/
export function dashToCamelCase(string) {
return string.replace(/-(\w)/g, (_, c) => c.toUpperCase())
}