wraplet 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +19 -0
- package/dist/AbstractWraplet.d.ts +16 -0
- package/dist/errors.d.ts +4 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/types/Utils.d.ts +9 -0
- package/dist/types/Wraplet.d.ts +3 -0
- package/dist/types/WrapletChildDefinition.d.ts +10 -0
- package/dist/types/WrapletChildren.d.ts +5 -0
- package/dist/types/WrapletChildrenMap.d.ts +4 -0
- package/dist/types/global.d.ts +6 -0
- package/package.json +42 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2024-present Łukasz Zaroda
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is furnished
|
|
8
|
+
to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { WrapletChildrenMap } from "./types/WrapletChildrenMap";
|
|
2
|
+
import { WrapletChildren } from "./types/WrapletChildren";
|
|
3
|
+
import { Wraplet } from "./types/Wraplet";
|
|
4
|
+
import { DeepWriteable } from "./types/Utils";
|
|
5
|
+
export declare abstract class AbstractWraplet<T extends WrapletChildrenMap = {}, E extends Element = Element> implements Wraplet {
|
|
6
|
+
protected element: E;
|
|
7
|
+
isWraplet: true;
|
|
8
|
+
protected children: WrapletChildren<T>;
|
|
9
|
+
protected static debug: boolean;
|
|
10
|
+
constructor(element: E, mapAlterCallback?: ((map: DeepWriteable<T>) => void) | null);
|
|
11
|
+
protected abstract defineChildrenMap(): T;
|
|
12
|
+
protected instantiateChildren(map: T): WrapletChildren<T>;
|
|
13
|
+
protected createWraplet(wrapletClass: new (...args: any[]) => Wraplet, childElement: Element, args?: unknown[]): Wraplet;
|
|
14
|
+
private childTypeGuard;
|
|
15
|
+
protected static createWraplets<T extends AbstractWraplet<any> = never>(document: Document, additional_args: unknown[] | undefined, selector: string): T[];
|
|
16
|
+
}
|
package/dist/errors.d.ts
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.wraplet=t():e.wraplet=t()}(self,(()=>(()=>{"use strict";var e={d:(t,r)=>{for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{AbstractWraplet:()=>o});class r extends Error{}class n extends Error{}class o{constructor(e,t=null){this.element=e,this.isWraplet=!0;const r=this.defineChildrenMap();t&&t(r),this.children=this.instantiateChildren(r),this.element.wraplets||(this.element.wraplets=[]),this.element.wraplets.push(this)}instantiateChildren(e){const t={};for(const o in e){const s=e[o],i=s.selector,l=s.Class,a=s.args||[],c=s.multiple,u=s.required;if(!i){if(u)throw new n(`${this.constructor.name}: Child "${o}" cannot at the same be required and have no selector.`);t[o]=c?[]:null;continue}const h=this.element.querySelectorAll(i);if(0===h.length){if(u)throw new r(`${this.constructor.name}: Couldn't find an element for the wraplet "${o}". Selector used: "${i}".`);this.constructor.debug&&console.log(`${this.constructor.name}: Optional child '${o}' has not been found. Selector used: "${i}"`),t[o]=c?[]:null;continue}const d=[];for(const e of h)if(d.push(this.createWraplet(l,e,a)),!c)break;const f=c?d:d[0];if(c&&!f&&this.constructor.debug&&console.log(`${this.constructor.name}: no items for the multiple child '${o}' have been found. Selector used: "${i}"`),!this.childTypeGuard(f,o)){if(void 0===f)throw new Error(`${this.constructor.name}: Couldn't intantionate the "${o}" child. Selector used: "${i}".`);throw new Error(`${this.constructor.name}: Child value doesn't match the map. Value: ${f}. Expected: ${e[o].Class.name}`)}t[o]=f}return t}createWraplet(e,t,r=[]){return new e(...[t,...r])}childTypeGuard(e,t){const r=this.defineChildrenMap(),n=r[t].Class,o=r[t].required;return r[t].multiple?!!Array.isArray(e)&&(!o||e.every((e=>e instanceof n))):o?e instanceof n:e instanceof n||null===e}static createWraplets(e,t=[],r){if(this instanceof o)throw new Error("You cannot instantiate an abstract class.");const n=[],s=e.querySelectorAll(r);for(const e of s)n.push(new this(e,...t));return n}}return o.debug=!1,t})()));
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","mappings":"CAAA,SAA2CA,EAAMC,GAC1B,iBAAZC,SAA0C,iBAAXC,OACxCA,OAAOD,QAAUD,I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AAkB,GAAIxB,GAClD,GAAIV,gBAAgBJ,EAChB,MAAM,IAAIF,MAAM,6CAEpB,MAAMyC,EAAS,GACTC,EAAgBH,EAASd,iBAAiBT,GAChD,IAAK,MAAMZ,KAAWsC,EAClBD,EAAO5B,KAAK,IAAIP,KAAKF,KAAYoC,IAErC,OAAOC,CACX,E,OAEJvC,EAAgByB,OAAQ,E","sources":["webpack://wraplet/webpack/universalModuleDefinition","webpack://wraplet/webpack/bootstrap","webpack://wraplet/webpack/runtime/define property getters","webpack://wraplet/webpack/runtime/hasOwnProperty shorthand","webpack://wraplet/webpack/runtime/make namespace object","webpack://wraplet/./src/errors.ts","webpack://wraplet/./src/AbstractWraplet.ts"],"sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"wraplet\"] = factory();\n\telse\n\t\troot[\"wraplet\"] = factory();\n})(self, () => {\nreturn ","// The require scope\nvar __webpack_require__ = {};\n\n","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","export class MissingRequiredChildError extends Error {\n}\nexport class MapError extends Error {\n}\n","import { MapError, MissingRequiredChildError } from \"./errors\";\nexport class AbstractWraplet {\n constructor(element, mapAlterCallback = null) {\n this.element = element;\n this.isWraplet = true;\n const map = this.defineChildrenMap();\n if (mapAlterCallback) {\n mapAlterCallback(map);\n }\n this.children = this.instantiateChildren(map);\n if (!this.element.wraplets) {\n this.element.wraplets = [];\n }\n this.element.wraplets.push(this);\n }\n instantiateChildren(map) {\n const children = {};\n for (const id in map) {\n const item = map[id];\n const selector = item.selector;\n const wrapletClass = item.Class;\n const args = item.args || [];\n const multiple = item.multiple;\n const isRequired = item.required;\n if (!selector) {\n if (isRequired) {\n throw new MapError(`${this.constructor.name}: Child \"${id}\" cannot at the same be required and have no selector.`);\n }\n children[id] = multiple\n ? []\n : null;\n continue;\n }\n const childElements = this.element.querySelectorAll(selector);\n if (childElements.length === 0) {\n if (isRequired) {\n throw new MissingRequiredChildError(`${this.constructor.name}: Couldn't find an element for the wraplet \"${id}\". Selector used: \"${selector}\".`);\n }\n if (this.constructor.debug) {\n console.log(`${this.constructor.name}: Optional child '${id}' has not been found. Selector used: \"${selector}\"`);\n }\n children[id] = multiple\n ? []\n : null;\n continue;\n }\n const childWraplet = [];\n for (const childElement of childElements) {\n childWraplet.push(this.createWraplet(wrapletClass, childElement, args));\n if (!multiple) {\n break;\n }\n }\n const value = multiple\n ? childWraplet\n : childWraplet[0];\n if (multiple && !value && this.constructor.debug) {\n console.log(`${this.constructor.name}: no items for the multiple child '${id}' have been found. Selector used: \"${selector}\"`);\n }\n if (!this.childTypeGuard(value, id)) {\n if (typeof value === \"undefined\") {\n throw new Error(`${this.constructor.name}: Couldn't intantionate the \"${id}\" child. Selector used: \"${selector}\".`);\n }\n throw new Error(`${this.constructor.name}: Child value doesn't match the map. Value: ${value}. Expected: ${map[id][\"Class\"].name}`);\n }\n children[id] = value;\n }\n // Now we should have all properties set, so let's assert the final form.\n return children;\n }\n createWraplet(wrapletClass, childElement, args = []) {\n return new wrapletClass(...[...[childElement], ...args]);\n }\n childTypeGuard(variable, id) {\n const map = this.defineChildrenMap();\n const Class = map[id].Class;\n const isRequired = map[id].required;\n const isMultiple = map[id].multiple;\n if (isMultiple) {\n const isArray = Array.isArray(variable);\n if (!isArray) {\n return false;\n }\n if (isRequired) {\n return variable.every((value) => value instanceof Class);\n }\n return true;\n }\n if (isRequired) {\n return variable instanceof Class;\n }\n return variable instanceof Class || variable === null;\n }\n // We can afford \"any\" here because this method is only for the external usage, and external\n // callers don't need to know what map is the current wraplet using, as it's its internal\n // matter.\n static createWraplets(document, additional_args = [], selector) {\n if (this instanceof AbstractWraplet) {\n throw new Error(\"You cannot instantiate an abstract class.\");\n }\n const result = [];\n const foundElements = document.querySelectorAll(selector);\n for (const element of foundElements) {\n result.push(new this(element, ...additional_args));\n }\n return result;\n }\n}\nAbstractWraplet.debug = false;\n"],"names":["root","factory","exports","module","define","amd","self","__webpack_require__","definition","key","o","Object","defineProperty","enumerable","get","obj","prop","prototype","hasOwnProperty","call","Symbol","toStringTag","value","MissingRequiredChildError","Error","MapError","AbstractWraplet","constructor","element","mapAlterCallback","this","isWraplet","map","defineChildrenMap","children","instantiateChildren","wraplets","push","id","item","selector","wrapletClass","Class","args","multiple","isRequired","required","name","childElements","querySelectorAll","length","debug","console","log","childWraplet","childElement","createWraplet","childTypeGuard","variable","Array","isArray","every","createWraplets","document","additional_args","result","foundElements"],"sourceRoot":""}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AbstractWraplet } from "../AbstractWraplet";
|
|
2
|
+
export type WrapletChildDefinition<T extends AbstractWraplet = AbstractWraplet> = {
|
|
3
|
+
selector?: string;
|
|
4
|
+
Class: {
|
|
5
|
+
new (...args: any[]): T;
|
|
6
|
+
};
|
|
7
|
+
required: boolean;
|
|
8
|
+
multiple: boolean;
|
|
9
|
+
args?: unknown[];
|
|
10
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { WrapletChildrenMap } from "./WrapletChildrenMap";
|
|
2
|
+
import { InstantiableReturnType } from "./Utils";
|
|
3
|
+
export type WrapletChildren<T extends WrapletChildrenMap> = {
|
|
4
|
+
[id in keyof T]: T[id]["multiple"] extends true ? InstantiableReturnType<T[id]["Class"]>[] : T[id]["required"] extends true ? InstantiableReturnType<T[id]["Class"]> : InstantiableReturnType<T[id]["Class"]> | null;
|
|
5
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "wraplet",
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/lukasz-zaroda/wraplet.git"
|
|
10
|
+
},
|
|
11
|
+
"files": ["dist"],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"setup": "yarn install",
|
|
14
|
+
"build": "./node_modules/.bin/webpack --mode production --config ./webpack.config.js",
|
|
15
|
+
"dev:build": "./node_modules/.bin/webpack --mode development --config ./webpack.config.js",
|
|
16
|
+
"dev:watch": "./node_modules/.bin/webpack --mode development --watch --config ./webpack.config.js",
|
|
17
|
+
"lint": "tsc --noemit && npx eslint './src/**/*.ts' './tests/**/*.ts'",
|
|
18
|
+
"lint:fix": "npx eslint './src/**/*.ts' './tests/**/*.ts' --fix",
|
|
19
|
+
"tests": "jest"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"typescript": "^5.5.4",
|
|
25
|
+
"ts-loader": "^9.5.1",
|
|
26
|
+
"@eslint/js": "^9.9.1",
|
|
27
|
+
"eslint": "^9.9.1",
|
|
28
|
+
"globals": "^15.9.0",
|
|
29
|
+
"typescript-eslint": "^8.2.0",
|
|
30
|
+
"webpack": "^5.94.0",
|
|
31
|
+
"webpack-cli": "^5.1.4",
|
|
32
|
+
"prettier": "^3.3.3",
|
|
33
|
+
"eslint-config-prettier": "^9.1.0",
|
|
34
|
+
"eslint-plugin-prettier": "^5.2.1",
|
|
35
|
+
"prettier-eslint": "^16.3.0",
|
|
36
|
+
"jest": "^29.7.0",
|
|
37
|
+
"jest-cli": "^29.7.0",
|
|
38
|
+
"ts-jest": "^29.2.5",
|
|
39
|
+
"@types/jest": "^29.5.12",
|
|
40
|
+
"jest-environment-jsdom": "^29.7.0"
|
|
41
|
+
}
|
|
42
|
+
}
|