1.0.2-2 • Published 4 years ago

@knownout/utils v1.0.2-2

Weekly downloads
5
License
MIT
Repository
github
Last release
4 years ago

knownOut's utils

npm

Assembly of useful stuff containing various functions to simplify web applications creation

Constants

ConstantMethodsDescription
_.Timesecond, minute, hour, day, week, month, yearTime constants in milliseconds
_.formatterscodeString, displayStringList of fuctions for formatString method
// String format examle
_.formatString("sample-string", _.formatters.displayString); // Sample string
_.formatString("Sample string", _.formatters.codeString); // sample-string

Methods

MethodDescription
_.$ (selector: string | HTMLElement, from?: HTMLElement | null, count?: number): HTMLElementNext | HTMLElementNext[]Get one or more elements from DOM tree as HTMLElementNext or convert HTMLElement to HTMLNextElement
_.create (tag: string | HTMLElement, attributes?: object, parent?: HTMLElement): HTMLElementNextCreates an HTMLElementNext with provided tagName and attributes and append it to parent HTMLElement if provided or converts HTMLElement to HTMLElementNext (html and text in attributes list are abbreviations for innerHTML and innerText, to avoid this use "!" before attribute name: "!text", "!html")
_.fetch (url: string, method: string, body?: any, cacheKey?: string, expires?: number): PromiseAsynchronously sends fetch request with specific params to given URL and then returns and cache result to the storage
_.formatString (string: string, formatter: (str: string) => string): stringFormat string with provided formatter function
// Get all div.sample elements from DOM tree
let e = _.$("div.sample", null, -1);
e = _.$("div.sample", null, 10); // Get first 10 div.sample elements
// Create HTMLElementNext with innerText
const ne = _.create("span.title", { text: "Hello world" });
// Fetch data and cache it to the localStorage for one day
const toLS = await _.fetch("http://example.com/", "GET", null, "dataFromExample", _.Time.day);

// ... or to sessionStorage
const toSS = await _.fetch("http://example.com/", "GET", null, "dataFromExample");
// Cache from sessionStorage can be cleared by reloading with CTRL + F5

HTMLElementNext

HTMLElementNext is a type for extended html elements. All HTMLElementNext-specific methods can be written in chain

MethodDescription
attr (attributes: string | any, value?: string): anySet or get provided attribute from the element (html and text in attributes list are abbreviations for innerHTML and innerText, to avoid this use “!” before attribute name: “!text”, “!html”)
event (events: keyof HTMLElementEventMap | string | object, value?: Function): HTMLElementNextAppend event listener to the parent element
find (selector: string, all?: true): anyFinds all elements by selector in the parent element DOM tree (querySelector shortcut)
const e = _.$("div.sample").attr("text") // innerText of div.spample
    .attr("!text"); // div.sample text attribute
    
e.event("click", function () {
    console.log(this);
}); // Push this element to console on click

e.event({
    click: () => console.log("Hello world"),
    hover: function () { this.attr("hover", "true"); }
}); // Add multiple events
1.0.2-2

4 years ago

1.0.2-1

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago