Skip to content
Snippets Groups Projects
Commit 437c7d19 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

#32 #29

parent eaf6361d
No related branches found
No related tags found
No related merge requests found
......@@ -4,9 +4,40 @@
* @author schukai GmbH
*/
import {isSymbol} from "../../test/web/tests.js";
import {Monster} from '../namespace.js';
import {TokenList} from "../types/tokenlist.js";
import {validateInstance, validateString} from "../types/validate.js";
import {validateInstance, validatePrimitive, validateString} from "../types/validate.js";
import {ATTRIBUTE_OBJECTLINK} from "./constants.js";
function addObjectLink(element, symbol, object) {
if (element?.[symbol] === undefined) {
element[symbol] = new Set;
}
addAttributeToken(ATTRIBUTE_OBJECTLINK, symbol.toString());
element[symbol].add(object);
return element;
}
function removeObjectLink(element, symbol) {
validateInstance(element, HTMLElement);
validatePrimitive(symbol)
validateString(key)
if (this.element?.[symbol] === undefined) {
return element
}
removeAttributeToken(ATTRIBUTE_OBJECTLINK, symbol.toString());
delete this.element[symbol];
return element;
}
/**
......
......@@ -88,7 +88,7 @@ const ATTRIBUTE_UPDATER_BIND = ATTRIBUTE_PREFIX + 'bind';
* @type {string}
* @since 1.9.0
*/
const ATTRIBUTE_UPDATER_HASSYMBOL = ATTRIBUTE_PREFIX + 'hassymbol';
const ATTRIBUTE_OBJECTLINK = ATTRIBUTE_PREFIX + 'objectlink';
export {
......@@ -104,5 +104,5 @@ export {
ATTRIBUTE_UPDATER_INSERT_REFERENCE,
ATTRIBUTE_UPDATER_REMOVE,
ATTRIBUTE_UPDATER_BIND,
ATTRIBUTE_UPDATER_HASSYMBOL
ATTRIBUTE_OBJECTLINK
}
\ No newline at end of file
......@@ -18,6 +18,7 @@ import {
ATTRIBUTE_UPDATER_BIND,
ATTRIBUTE_UPDATER_HASSYMBOL
} from "../dom/constants.js";
import {addAttributeToken, removeAttributeToken} from "./attributes.js";
import {getDocument} from "./util.js";
/**
......@@ -107,13 +108,7 @@ class Updater extends Base {
*/
this.element = validateInstance(element, HTMLElement);
if (this.element?.[symbol] === undefined) {
this.element[symbol] = new Set;
}
this.element[symbol].add(this);
this.element.setAttribute(ATTRIBUTE_UPDATER_HASSYMBOL,symbol.toString());
addReference.call(this);
if (subject === undefined) subject = {}
if (!isInstance(subject, ProxyObserver)) {
......@@ -221,6 +216,7 @@ class Updater extends Base {
}
/**
* @private
* @param event
......
......@@ -89,6 +89,39 @@ function isPrimitive(value) {
return false;
}
/**
* Checks whether the value passed is a symbol
*
* You can call the method via the monster namespace `Monster.Types.isSymbol()`.
*
* ```
* <script type="module">
* import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.9.0/dist/modules/types/is.js';
* console.log(Monster.Types.isSymbol('2')) // ↦ false
* console.log(Monster.Types.isSymbol(Symbol('test')) // ↦ true
* </script>
* ```
*
* Alternatively, you can also integrate this function individually.
*
* ```
* <script type="module">
* import {isSymbol} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.9.0/dist/modules/types/is.js';
* console.log(isSymbol(Symbol('a'))) // ↦ true
* console.log(isSymbol([]) // ↦ false
* </script>
* ```
*
* @param {*} value
* @returns {boolean}
* @since 1.9.0
* @copyright schukai GmbH
* @memberOf Monster/Types
*/
function isSymbol(value) {
return ('symbol' === typeof value) ? true : false;
}
/**
* Checks whether the value passed is a boolean
*
......@@ -362,5 +395,17 @@ function isInteger(value) {
}
Monster.assignToNamespace('Monster.Types', isPrimitive, isBoolean, isString, isObject, isArray, isFunction, isIterable, isInteger);
export {Monster, isPrimitive, isBoolean, isString, isObject, isInstance, isArray, isFunction, isIterable, isInteger}
Monster.assignToNamespace('Monster.Types', isPrimitive, isBoolean, isString, isObject, isArray, isFunction, isIterable, isInteger, isSymbol);
export {
Monster,
isPrimitive,
isBoolean,
isString,
isObject,
isInstance,
isArray,
isFunction,
isIterable,
isInteger,
isSymbol
}
......@@ -14,7 +14,8 @@ import {
isFunction,
isIterable,
isInstance,
isInteger
isInteger,
isSymbol
} from './is.js';
/**
......@@ -26,7 +27,7 @@ import {
* <script type="module">
* import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.9.0/dist/modules/types/validate.js';
* console.log(Monster.Types.validateIterable('2')) // ↦ TypeError
* console.log(Monster.Types.validateIterable([])) // ↦ undefined
* console.log(Monster.Types.validateIterable([])) // ↦ value
* </script>
* ```
*
......@@ -36,7 +37,7 @@ import {
* <script type="module">
* import {validateIterable} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.9.0/dist/modules/types/validate.js';
* console.log(validateIterable('2')) // ↦ TypeError
* console.log(validateIterable([])) // ↦ undefined
* console.log(validateIterable([])) // ↦ value
* </script>
* ```
*
......@@ -65,7 +66,7 @@ function validateIterable(value) {
* ```
* <script type="module">
* import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.9.0/dist/modules/types/validate.js';
* console.log(Monster.Types.validatePrimitive('2')) // ↦ undefined
* console.log(Monster.Types.validatePrimitive('2')) // ↦ value
* console.log(Monster.Types.validatePrimitive([])) // ↦ TypeError
* </script>
* ```
......@@ -75,7 +76,7 @@ function validateIterable(value) {
* ```
* <script type="module">
* import {validatePrimitive} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.9.0/dist/modules/types/validate.js';
* console.log(validatePrimitive('2')) // ↦ undefined
* console.log(validatePrimitive('2')) // ↦ value
* console.log(validatePrimitive([])) // ↦ TypeError
* </script>
* ```
......@@ -105,7 +106,7 @@ function validatePrimitive(value) {
* ```
* <script type="module">
* import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.9.0/dist/modules/types/validate.js';
* console.log(Monster.Types.validateBoolean(true)) // ↦ undefined
* console.log(Monster.Types.validateBoolean(true)) // ↦ value
* console.log(Monster.Types.validateBoolean('2')) // ↦ TypeError
* console.log(Monster.Types.validateBoolean([])) // ↦ TypeError
* </script>
......@@ -116,7 +117,7 @@ function validatePrimitive(value) {
* ```
* <script type="module">
* import {validateBoolean} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.9.0/dist/modules/types/validate.js';
* console.log(validateBoolean(false)) // ↦ undefined
* console.log(validateBoolean(false)) // ↦ value
* console.log(validateBoolean('2')) // ↦ TypeError
* console.log(validateBoolean([])) // ↦ TypeError
* </script>
......@@ -145,7 +146,7 @@ function validateBoolean(value) {
* ```
* <script type="module">
* import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.9.0/dist/modules/types/validate.js';
* console.log(Monster.Types.validateString('2')) // ↦ undefined
* console.log(Monster.Types.validateString('2')) // ↦ value
* console.log(Monster.Types.validateString([])) // ↦ TypeError
* </script>
* ```
......@@ -155,7 +156,7 @@ function validateBoolean(value) {
* ```
* <script type="module">
* import {validateString} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.9.0/dist/modules/types/validate.js';
* console.log(validateString('2')) // ↦ undefined
* console.log(validateString('2')) // ↦ value
* console.log(validateString([])) // ↦ TypeError
* </script>
* ```
......@@ -183,7 +184,7 @@ function validateString(value) {
* ```
* <script type="module">
* import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.9.0/dist/modules/types/validate.js';
* console.log(Monster.Types.validateObject({})) // ↦ undefined
* console.log(Monster.Types.validateObject({})) // ↦ value
* console.log(Monster.Types.validateObject('2')) // ↦ TypeError
* console.log(Monster.Types.validateObject([])) // ↦ TypeError
* </script>
......@@ -194,7 +195,7 @@ function validateString(value) {
* ```
* <script type="module">
* import {validateObject} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.9.0/dist/modules/types/validate.js';
* console.log(validateObject({})) // ↦ undefined
* console.log(validateObject({})) // ↦ value
* console.log(validateObject('2')) // ↦ TypeError
* console.log(validateObject([])) // ↦ TypeError
* </script>
......@@ -222,7 +223,7 @@ function validateObject(value) {
* ```
* <script type="module">
* import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.9.0/dist/modules/types/validate.js';
* console.log(Monster.Types.validateInstance({}, Object)) // ↦ undefined
* console.log(Monster.Types.validateInstance({}, Object)) // ↦ value
* console.log(Monster.Types.validateInstance('2', Object)) // ↦ TypeError
* console.log(Monster.Types.validateInstance([], Object)) // ↦ TypeError
* </script>
......@@ -233,7 +234,7 @@ function validateObject(value) {
* ```
* <script type="module">
* import {validateInstance} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.9.0/dist/modules/types/validate.js';
* console.log(validateInstance({}, Object)) // ↦ undefined
* console.log(validateInstance({}, Object)) // ↦ value
* console.log(validateInstance('2', Object)) // ↦ TypeError
* console.log(validateInstance([], Object)) // ↦ TypeError
* </script>
......@@ -271,7 +272,7 @@ function validateInstance(value, instance) {
* <script type="module">
* import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.9.0/dist/modules/types/validate.js';
* console.log(Monster.Types.validateArray('2')) // ↦ TypeError
* console.log(Monster.Types.validateArray([])) // ↦ undefined
* console.log(Monster.Types.validateArray([])) // ↦ value
* </script>
* ```
*
......@@ -281,7 +282,7 @@ function validateInstance(value, instance) {
* <script type="module">
* import {validateArray} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.9.0/dist/modules/types/validate.js';
* console.log(validateArray('2')) // ↦ TypeError
* console.log(validateArray([])) // ↦ undefined
* console.log(validateArray([])) // ↦ value
* </script>
* ```
*
......@@ -299,6 +300,43 @@ function validateArray(value) {
return value
}
/**
* This method checks if the type matches the symbol type. this function is identical to isSymbol() except that a TypeError is thrown.
*
* You can call the method via the monster namespace `Monster.Types.validateSymbol()`.
*
* ```
* <script type="module">
* import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.9.0/dist/modules/types/validate.js';
* console.log(Monster.Types.validateSymbol('2')) // ↦ TypeError
* console.log(Monster.Types.validateSymbol([])) // ↦ value
* </script>
* ```
*
* Alternatively, you can also integrate this function individually.
*
* ```
* <script type="module">
* import {validateSymbol} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.9.0/dist/modules/types/validate.js';
* console.log(validateSymbol('2')) // ↦ TypeError
* console.log(validateSymbol()) // ↦ value
* </script>
* ```
*
* @param {*} value
* @return {*}
* @since 1.9.0
* @copyright schukai GmbH
* @memberOf Monster/Types
* @throws {TypeError} value is not an symbol
*/
function validateSymbol(value) {
if (!isSymbol(value)) {
throw new TypeError('value is not an symbol')
}
return value
}
/**
* This method checks if the type matches the function type. this function is identical to isFunction() except that a TypeError is thrown.
*
......@@ -307,7 +345,7 @@ function validateArray(value) {
* ```
* <script type="module">
* import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.9.0/dist/modules/types/validate.js';
* console.log(Monster.Types.validateFunction(()=>{})) // ↦ undefined
* console.log(Monster.Types.validateFunction(()=>{})) // ↦ value
* console.log(Monster.Types.validateFunction('2')) // ↦ TypeError
* console.log(Monster.Types.validateFunction([])) // ↦ TypeError
* </script>
......@@ -318,7 +356,7 @@ function validateArray(value) {
* ```
* <script type="module">
* import {validateFunction} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.9.0/dist/modules/types/validate.js';
* console.log(validateFunction(()=>{})) // ↦ undefined
* console.log(validateFunction(()=>{})) // ↦ value
* console.log(validateFunction('2')) // ↦ TypeError
* console.log(validateFunction([])) // ↦ TypeError
* </script>
......@@ -348,7 +386,7 @@ function validateFunction(value) {
* import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.9.0/dist/modules/types/validate.js';
* console.log(Monster.Types.validateInteger(true)) // ↦ TypeError
* console.log(Monster.Types.validateInteger('2')) // ↦ TypeError
* console.log(Monster.Types.validateInteger(2)) // ↦ undefined
* console.log(Monster.Types.validateInteger(2)) // ↦ value
* </script>
* ```
*
......@@ -359,7 +397,7 @@ function validateFunction(value) {
* import {validateFunction} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.9.0/dist/modules/types/validate.js';
* console.log(validateInteger(true)) // ↦ TypeError
* console.log(validateInteger('2')) // ↦ TypeError
* console.log(validateInteger(2)) // ↦ undefined
* console.log(validateInteger(2)) // ↦ value
* </script>
* ```
*
......@@ -388,5 +426,6 @@ export {
validateArray,
validateFunction,
validateIterable,
validateInteger
validateInteger,
validateSymbol
}
......@@ -8,7 +8,8 @@ import {
isInstance,
isFunction,
isIterable,
isInteger
isInteger,
isSymbol
} from "../../../source/types/is.js";
......@@ -50,6 +51,34 @@ describe('Is', function () {
});
});
describe('.isSymbol()', function () {
[
['test1', false],
[undefined, false],
[null, false],
[() => {
}, false],
[2, false],
[parseInt("a"), false],
[false, false],
[true, false],
[4.5, false],
[{}, false],
[[1, 2, 3], false],
[Symbol("foo"), true],
].forEach(function (data) {
let a = data.shift()
let b = data.shift()
it('is.isSymbol(' + typeof a + ') should return ' + b, function () {
expect(isSymbol(a)).is.equal(b)
});
});
});
describe('.isInteger()', function () {
......
......@@ -8,7 +8,8 @@ import {
validateFunction,
validateInteger,
validateInstance,
validateIterable
validateIterable,
validateSymbol
} from "../../../source/types/validate.js"
import {Base} from "../../../source/types/base.js"
......@@ -90,6 +91,43 @@ describe('validate', function () {
});
});
describe('.validateSymbol()', function () {
[
['test1', false],
[undefined, false],
[null, false],
[() => {
}, false],
[2, false],
[false, false],
[true, false],
[4.5, false],
[{}, false],
[[1, 2, 3], false],
[Symbol("foo"), true],
].forEach(function (data) {
let a = data.shift()
let b = data.shift()
if (b === false) {
it('.validateSymbol(' + typeof a + ') should throw TypeException ' + b, function () {
expect(() => validateSymbol(a)).to.throw(TypeError);
});
} else {
it('.validateSymbol(' + typeof a + ') should not throw TypeException ' + b, function () {
expect(validateSymbol(a)).to.be.equal(a);
});
}
});
});
describe('.validateIterable()', function () {
......
......@@ -5,6 +5,7 @@ import "../cases/logging/handler.js";
import "../cases/logging/logentry.js";
import "../cases/dom/handle.js";
import "../cases/dom/updater.js";
import "../cases/dom/attributes.js";
import "../cases/dom/template.js";
import "../cases/dom/util.js";
import "../cases/dom/assembler.js";
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment