Skip to content

Commit

Permalink
MakeElement class rewrite and package.json update
Browse files Browse the repository at this point in the history
  • Loading branch information
knownout committed Mar 21, 2022
1 parent ee338e8 commit 0ba70a9
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 17,001 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/node_modules
/dist
/package/dist
package-lock.json
16,932 changes: 0 additions & 16,932 deletions package-lock.json

This file was deleted.

11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
{
"name": "@knownout/lib",
"version": "0.0.3",
"version": "0.0.0",
"author": "Alexandr <re-knownout> knownout@hotmail.com",
"license": "AGPL-3.0",
"description": "Utility functions library",
"scripts": {
"serve": "webpack-dev-server --config webpack.config.js",
"build:package": "webpack --config webpack.package.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/re-knownout/lib.git",
"directory": "src/package"
},
"bugs": {
"url": "https://github.com/re-knownout/lib/issues"
},
"homepage": "https://github.com/re-knownout/lib#readme",
"devDependencies": {
"@types/jest": "^27.4.1",
"@types/react": "^17.0.41",
Expand Down
92 changes: 34 additions & 58 deletions package/bin/classes/MakeElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,84 +61,60 @@ export default class MakeElement<K extends keyof HTMLElementTagNameMap>
public set textContent (value: string) { this.native.textContent = value; }

/**
* Access to element setter functions.
* @return {
* {
* html: (value: string) => MakeElement,
* textContent: (value: string) => MakeElement,
* text: (value: string) => MakeElement}
* } element setters.
* Update element innerText attribute.
* @param {string} value innerText attribute value.
* @return {MakeElement}
*/
public setInnerText (value: string): this {
this.native.innerText = value;
return this;
}

/**
* Update element innerHTML attribute.
* @param {string} value innerHTML attribute value.
* @return {MakeElement}
*/
public get set () {
const self = this;
const fn = (cb: Function) => {
cb();
return self;
};

return {
/**
* Set element innerText attribute value
* @param {string} value new innerText value
*/
text: (value: string) => fn(() => self.native.innerText = value),

/**
* Set element innerHTML attribute value
* @param {string} value new innerHTML value
*/
html: (value: string) => fn(() => self.native.innerHTML = value),

/**
* Set element textContent attribute value
* @param {string} value new innerHTML value
*/
textContent: (value: string) => fn(() => self.native.textContent = value)
};
public setInnerHTML (value: string): this {
this.native.innerHTML = value;
return this;
}

/**
* Update element textContent attribute.
* @param {string} value textContent attribute value.
* @return {MakeElement}
*/
public setTextContent (value: string): this {
this.native.textContent = value;
return this;
}

/**
* Access to removing functions.
* @return {{attribute: (key: string) => MakeElement, element: () => void}} removing functions.
* Remove native element attribute or element itself.
* @param {Element | string} object attribute name or element.
*/
public get remove () {
const self = this;
const fn = (cb: Function) => {
cb();
return self;
};

return {
/**
* Remove element attribute by its name.
* @param {string} key attribute name.
* @return {MakeElement}
*/
attribute: (key: string) => fn(() => self.native.removeAttribute(key)),

/**
* Delete the element itself
*/
element: () => self.native.remove()
};
public remove (object: Element | string): void {
if (typeof object === "string") this.native.removeAttribute(object);
else object.remove();
}

/**
* Get element attribute value by attribute name.
* @param {string} key attribute name.
* @return {string} attribute value.
*/
public attribute (key: string): string
public attr (key: string): string

/**
* Set element attribute value by attribute name.
* @param {string} key attribute name.
* @param {string} value new attribute value.
* @return {MakeElement}
*/
public attribute (key: string, value: string): MakeElement<K>;
public attr (key: string, value: string): MakeElement<K>;

public attribute (key: string, value?: string): MakeElement<K> | string {
public attr (key: string, value?: string): MakeElement<K> | string {
if (value) this.native.setAttribute(key, value);
else return this.native.getAttribute(key) || String();

Expand Down
18 changes: 9 additions & 9 deletions package/bin/classes/tests/MakeElement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ describe("MakeElement class tests", () => {
const element = new MakeElement("div");

it("Attribute method test", () => {
element.attribute("attr", "value-1");
element.attr("attr", "value-1");

expect(element.native.getAttribute("attr")).toEqual("value-1");
expect(element.attribute("attr")).toEqual("value-1");
expect(element.attr("attr")).toEqual("value-1");
});

it("Setters test", () => {
element.set.text("Hello world!");
element.setInnerText("Hello world!");
expect(element.native.innerText).toEqual("Hello world!");

element.set.html("<b>Hello world!</b>");
element.setInnerHTML("<b>Hello world!</b>");
expect(element.native.innerHTML).toEqual("<b>Hello world!</b>");

element.set.textContent("Hello world!");
element.setInnerText("Hello world!");
expect(element.native.textContent).toEqual("Hello world!");
});

it("Special attributes test", () => {
element.set.html("<b>Hello world!</b>");
element.setInnerHTML("<b>Hello world!</b>");

expect(element.html).toEqual("<b>Hello world!</b>");
expect(element.text).toEqual("Hello world!");
Expand All @@ -46,10 +46,10 @@ describe("MakeElement class tests", () => {
});

it("Removing functions test", () => {
element.remove.attribute("attr");
element.remove("attr");

expect(element.attribute("attr")).toEqual(String());
element.remove.element();
expect(element.attr("attr")).toEqual(String());
element.remove(element.native);

expect(element.native).not.toBeUndefined();
});
Expand Down
6 changes: 6 additions & 0 deletions package/bin/classes/tests/StringExtractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ describe("StringExtractor class tests", () => {
const { extracted, entry } = extractor.attach(/#[a-z]+/g).extract;

expect(Object.keys(extracted)).toContain("#");

// @ts-ignore
expect(extracted["#"][0]).toEqual("weird");
expect(entry).toEqual("Hello world!");
});
Expand All @@ -20,14 +22,18 @@ describe("StringExtractor class tests", () => {
const extractor = new StringExtractor("Hello #weird +very +world!");
extractor.attach(/#[a-z]+/g).extract;

// @ts-ignore
expect(Object.assign({}, extractor.extracted)["#"][0]).toEqual("weird");
expect(extractor.entry).toEqual("Hello +very +world!");

extractor.attach(/\+[a-z]+/g).extract;

// @ts-ignore
expect(extractor.extracted["+"][0]).toEqual("very");
// @ts-ignore
expect(extractor.extracted["+"][1]).toEqual("world");

// @ts-ignore
expect(extractor.extracted["#"][0]).toEqual("weird");
expect(extractor.entry).toEqual("Hello !");
});
Expand Down
2 changes: 1 addition & 1 deletion package/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@knownout/lib",
"version": "0.0.1-0",
"version": "0.0.5",
"author": "Alexandr <re-knownout> knownout@hotmail.com",
"license": "AGPL-3.0",
"description": "Utility functions library",
Expand Down

0 comments on commit 0ba70a9

Please sign in to comment.