vanilla-website-utils
v1.18.8
Published
Vanilla JS utility functions for daily website usage
Downloads
74
Maintainers
Readme
Vanilla-website-utils
Vanilla JS utility functions for daily website usage
Feeback
If you are using or are interested in this module, please send me some Feedback. Any comment, request or critic is welcome! [email protected]
Install
npm install vanilla-website-utilsUsage:
<script src="../node_modules/vanilla-website-utils/dist/vanilla-website-utils.js"></script>Example Usage:
var vu = new Vanilla_website_utils();
window.onload = async function()
{
const host = await vu.get_host();
const v = await vu.get_parameters();
console.log(host);
console.log(v);
};
Functions:
<form>
<input id="test" class="test" />
</form> await vwu.autoload_textfield("http://127.0.0.1:8051/book_authors_name?f=data&_max=100",'test','test')Clone the Source via SSH:
git clone [email protected]:myridia/vanilla-website-utils.gitVanilla-website-utils
GPL licenses A module for Vanilla-website-utils
- Vanilla-website-utils
- module.exports#extract_anchor_value(str) ⇒ str ⏏
- module.exports#shuffle_array(array, length) ⇒ array ⏏
- module.exports#remove_tags() ⇒ string ⏏
- module.exports#is_number(value) ⇒ boolean ⏏
- module.exports#round(number, decimal) ⇒ float ⏏
- module.exports#get_month_back(yrs) ⇒ array ⏏
- module.exports#csv_file_to_array(file) ⇒ array ⏏
- module.exports#clear_textarea() ⏏
- module.exports#sort_object(obj, sort_order) ⇒ array ⏏
- module.exports#from_112_to_date(str, add_days) ⇒ object ⏏
- module.exports#date_to_112(_date) ⇒ string ⏏
- module.exports#month_list(month_back, revert, format) ⇒ arry ⏏
- module.exports#get_host(ext) ⇒ string ⏏
- module.exports#clean_url_parameters() ⇒ string ⏏
- module.exports#get_parameters(url) ⇒ object ⏏
- module.exports#get_select_text_by_value(select, value) ⇒ string ⏏
- module.exports#add_parameters(url, parameters) ⇒ string ⏏
- module.exports#afetch_all(callback, ...urls) ⇒ array ⏏
- module.exports#wait_for_all(...ps) ⇒ promise ⏏
- module.exports#afetch(i) ⇒ array ⏏
- module.exports#handle_rejection(p) ⇒ objet ⏏
- module.exports#aget_api(url, user, password, token) ⇒ object ⏏
- module.exports#aput_api(url, data, content_type) ⇒ object ⏏
- module.exports#apost_api(url, data, content_type) ⇒ object ⏏
- module.exports#post_api(url, my_callback, _data) ⇒ object ⏏
- module.exports#_email_validator(email) ⇒ bolan ⏏
- module.exports#email_validator(email) ⇒ bolan ⏏
- module.exports#get_url_parameter(name) ⇒ string ⏏
- module.exports#get_site() ⇒ string ⏏
- module.exports#autocomplete_textfield(setting, search) ⏏
- module.exports#autocomplete_select(setting) ⏏
- module.exports#_set_textfield() ⏏
- module.exports#autoload_textfield() ⏏
- module.exports#autocomplete() ⏏
- module.exports#post_textfield_rows(id, url, callback) ⇒ callback ⏏
- module.exports#get_form_data(form) ⇒ array ⏏
- module.exports#s_fill_select(select_obj, data) ⏏
- module.exports#fill_select(select_obj, data, selected, copy2clipboard, log) ⏏
- module.exports#pad(number) ⇒ string ⏏
- module.exports#s_set_selected(select_obj, data) ⏏
- module.exports#set_selected() ⏏
module.exports#extract_anchor_value(str) ⇒ str ⏏
Kind: Exported function
Returns: str - - extract anchor value
| Param | Type | Description | | --- | --- | --- | | str | str | anchor string |
Example
var vwu = new Vanilla_website_utils();
let str = "<a href=www.data.com>123.321</a>";
let v = vwu.extract_anchor_value(str);module.exports#shuffle_array(array, length) ⇒ array ⏏
Kind: Exported function
Returns: array - - new shuffled array
| Param | Type | Default | Description | | --- | --- | --- | --- | | array | array | | array to be shuffled | | length | int | 0 | length of the new returning array |
Example
var vwu = new Vanilla_website_utils();
const array = ["a","b","c","d","e","f","g","h","i"];
const new_array = await vwu.shuffle_array(array);module.exports#remove_tags() ⇒ string ⏏
Kind: Exported function
Returns: string - - string with the tags removed
| Param | Type | Description | | --- | --- | --- | | | string | string what contains html tags |
Example
var vwu = new Vanilla_website_utils();
var new_string = vwu.remove_tags(string)module.exports#is_number(value) ⇒ boolean ⏏
Kind: Exported function
Returns: boolean - - returns true for number or float
| Param | Type | Description | | --- | --- | --- | | value | string | string to check if its a number or float |
Example
var vwu = new Vanilla_website_utils();
document.querySelector("#input").addEventListener("change", function(){
let r = vwu.is_number("12,16346");
},false)module.exports#round(number, decimal) ⇒ float ⏏
Kind: Exported function
Returns: float - - rounded float number
| Param | Type | Default | Description | | --- | --- | --- | --- | | number | float | | float number to round | | decimal | integer | 0 | level of decimal to round |
Example
var vwu = new Vanilla_website_utils();
document.querySelector("#input").addEventListener("change", function(){
let arr = vwu.round(12,16346,2);
},false)module.exports#get_month_back(yrs) ⇒ array ⏏
Kind: Exported function
Returns: array - - file object with month back in time with the starting and end date of a 112 date format
| Param | Type | Default | Description | | --- | --- | --- | --- | | yrs | int | 0 | number of years back in time - default is 0, what brings only the actual year |
Example
var vwu = new Vanilla_website_utils();
months = vwu.get_month_back(5)module.exports#csv_file_to_array(file) ⇒ array ⏏
Kind: Exported function
Returns: array - - the collected data from the csv file into an array
| Param | Type | Description | | --- | --- | --- | | file | object | file object from a HTML website |
Example
var vwu = new Vanilla_website_utils();
document.querySelector("#input").addEventListener("change", function(){
let arr = vwu.file_to_array(this.files[0]);
},false)module.exports#clear_textarea() ⏏
Kind: Exported function
Example
var vwu = new Vanilla_website_utils();
vwu.clear_textarea()module.exports#sort_object(obj, sort_order) ⇒ array ⏏
Kind: Exported function
| Param | Type | Default | Description | | --- | --- | --- | --- | | obj | object | | javascript object | | sort_order | string | "desc" | desc or asc (default desc) |
Example
var vwu = new Vanilla_website_utils();
let obj = {a:100,b:50,c:75,d:1};
let a = vwu.sort_object('desc'); module.exports#from_112_to_date(str, add_days) ⇒ object ⏏
Kind: Exported function
Returns: object - - date object
| Param | Type | Default | Description | | --- | --- | --- | --- | | str | str | | a 112 string type date | | add_days | int | 0 | add days or remove by adding -1 or 1 |
Example
var vwu = new Vanilla_website_utils();
let date = vwu.112_to_date('20240601'); module.exports#date_to_112(_date) ⇒ string ⏏
Kind: Exported function
Returns: string - - in 112 format like 20230515
| Param | Type | Description | | --- | --- | --- | | _date | object | dateobject |
Example
var vwu = new Vanilla_website_utils();
let date = new Date();
let iso_112 = vwu.date_to_112(date); module.exports#month_list(month_back, revert, format) ⇒ arry ⏏
Kind: Exported function
Returns: arry - - list of month in format like 10.2022
| Param | Type | Default | Description | | --- | --- | --- | --- | | month_back | int | 12 | number of month back | | revert | bolean | false | revert order - Default is false | | format | string | "`${j}.${i}`" | return format of the month year pair - Default {j}.{i} like 09.2022 | j = month | i =year | |
Example
var vwu = new Vanilla_website_utils();
let data = await vwu.month_list(60,true,"`${j}.${i}`");module.exports#get_host(ext) ⇒ string ⏏
Kind: Exported function
Returns: string - - the hostname
| Param | Type | Default | Description | | --- | --- | --- | --- | | ext | string | "html" | extentison |
Example
var vwu = new Vanilla_website_utils();
let host = await vwu.get_host();module.exports#clean_url_parameters() ⇒ string ⏏
Kind: Exported function
Returns: string - removed empty parameters -parameters with an empty string or none
Example
var vwu = new Vanilla_website_utils();
url in the addressbar = "http://foo.com?key=bar&key2=&key3=barbar"
url = await vwu.add_parameters();
await vwu.clean_url_parameters();
the addressbar will be changed to "http://foo.com?key3=barbar" module.exports#get_parameters(url) ⇒ object ⏏
Kind: Exported function
Returns: object - json pair key/value -- new line value will be return as array!
| Param | Type | Description | | --- | --- | --- | | url | string | url |
Example
var vwu = new Vanilla_website_utils();
let v = await vwu.get_parameters();module.exports#get_select_text_by_value(select, value) ⇒ string ⏏
Kind: Exported function
Returns: string - text
| Param | Type | Description | | --- | --- | --- | | select | object | the DOM select object | | value | string | value |
Example
var vwu = new Vanilla_website_utils();
let mytext = await vwu.get_select_text_by_value(document.querySelector("#resource"), v['resource'],v['resource']);module.exports#add_parameters(url, parameters) ⇒ string ⏏
Kind: Exported function
Returns: string - url plus the new parameters
| Param | Type | Description | | --- | --- | --- | | url | string | original url | | parameters | string | json like {'foo':'bar'} |
Example
var vwu = new Vanilla_website_utils();
filter = {"foo":"bar"};
url = await vwu.add_parameters(url, filter);module.exports#afetch_all(callback, ...urls) ⇒ array ⏏
This function allows you to call multiple API Get URLs asynchronously and wait for all results. Once all results are received, an array of JSON object results is returned.
Kind: Exported function
Returns: array - - returns 3 nested arrays, main array, counter number and the last nested array includes a JSON object of the actual result
| Param | Type | Description | | --- | --- | --- | | callback | function | The callback function to return the full result | | ...urls | string | list of URLs, using the spread syntax |
Example
var vwu = new Vanilla_website_utils();
const url = "http://example.com/api/foo"
vwu.aget_api(call_me_back,url, url, url, url);
function call_me_back(msg) {
console.log(msg);
}module.exports#wait_for_all(...ps) ⇒ promise ⏏
Kind: Exported function
Returns: promise - - returns a promise
| Param | Type | Description | | --- | --- | --- | | ...ps | functions | list of functions to process |
Example
this.wait_for_all(...function_array).then((results) => callback(results));module.exports#afetch(i) ⇒ array ⏏
Kind: Exported function
Returns: array - - returns and array with the counter and JSON object result
| Param | Type | Description | | --- | --- | --- | | i | string | url for the GET call |
Example
this.wait_for_all(...function_array).then((results) => callback(results));module.exports#handle_rejection(p) ⇒ objet ⏏
Kind: Exported function
Returns: objet - - returns an error
| Param | Type | Description | | --- | --- | --- | | p | fuction | promise fuction to process |
Example
//used by the model wait_for all, see above
wait_for_all(...ps) {
return Promise.all(ps.map(this.handle_rejection));
}module.exports#aget_api(url, user, password, token) ⇒ object ⏏
Kind: Exported function
Returns: object - json
| Param | Type | Description | | --- | --- | --- | | url | string | api url | | user | string | user - for Basic Authorization | | password | string | password - for Basic Authorization | | token | string | token - for Bearer Authorization |
Example
var vwu = new Vanilla_website_utils();
let res = await vwu.aget_api(url);module.exports#aput_api(url, data, content_type) ⇒ object ⏏
Kind: Exported function
Returns: object - json
| Param | Type | Default | Description | | --- | --- | --- | --- | | url | string | | api url | | data | object | | data | | content_type | string | "application/json" | content type of the sending data optional - default is "application/json" |
Example
let data = {"foo":"bar"}
var vwu = new Vanilla_website_utils();
let res = await vwu.apost_api(url, data, "application/json");module.exports#apost_api(url, data, content_type) ⇒ object ⏏
Kind: Exported function
Returns: object - json
| Param | Type | Default | Description | | --- | --- | --- | --- | | url | string | | api url | | data | object | | data | | content_type | string | "application/json" | content type of the sending data optional - default is "application/json" |
Example
var vwu = new Vanilla_website_utils();
let res = await vwu.apost_api(url, data, "application/json");module.exports#post_api(url, my_callback, _data) ⇒ object ⏏
Kind: Exported function
Returns: object - json
| Param | Type | Description | | --- | --- | --- | | url | string | api url | | | object | data | | my_callback | string | name of the callback function | | _data | string | content type of the sending data optional - default is "application/json" |
Example
var vwu = new Vanilla_website_utils();
let data = {"foo":"bar"}
let ret = vwu.post_api('http://foo.bar/api','myfuncion','application/json') module.exports#_email_validator(email) ⇒ bolan ⏏
Kind: Exported function
Returns: bolan - true if ok and false if bad email
| Param | Type | Description | | --- | --- | --- | | email | string | email |
Example
var vwu = new Vanilla_website_utils();
const ok = vwu._email_validator([email protected])module.exports#email_validator(email) ⇒ bolan ⏏
Kind: Exported function
Returns: bolan - true if ok and false if bad email
| Param | Type | Description | | --- | --- | --- | | email | string | email |
Example
var vwu = new Vanilla_website_utils();
const ok = await vwu._email_validator([email protected])module.exports#get_url_parameter(name) ⇒ string ⏏
Kind: Exported function
Returns: string - url parameter
| Param | Type | Description | | --- | --- | --- | | name | string | key of the url parameter |
Example
var vwu = new Vanilla_website_utils();
const from = await vwu.get_url_parameter('from')module.exports#get_site() ⇒ string ⏏
Kind: Exported function
Returns: string - sitename
Example
var vwu = new Vanilla_website_utils();
let site = vwu.get_sitemodule.exports#autocomplete_textfield(setting, search) ⏏
Kind: Exported function
| Param | Type | Description | | --- | --- | --- | | setting | object | see example | | search | | in object can be begining(default) or inline |
Example
var vwu = new Vanilla_website_utils();
await vwu.autocomplete_textfield({
url: api + '/code/name',
onetimeload: true,
dom_id: 'search',
name: 'search',
append_to: '#nav-search',
min_key_length: 2
search: inline
});
url: api + '/code/name',
onetimeload: true,
dom_id: 'search',
name: 'search',
append_to: '#nav-search',
min_key_length: 2
search: beginning
});module.exports#autocomplete_select(setting) ⏏
Kind: Exported function
| Param | Type | Description | | --- | --- | --- | | setting | object | see example |
Example
var vwu = new Vanilla_website_utils();
await vwu.autocomplete_select({
url: api + '/code/name',
onetimeload: true,
dom_id: 'search',
name: 'search',
});module.exports#_set_textfield() ⏏
Kind: Exported function
module.exports#autoload_textfield() ⏏
Kind: Exported function
module.exports#autocomplete() ⏏
Kind: Exported function
module.exports#post_textfield_rows(id, url, callback) ⇒ callback ⏏
Kind: Exported function
Returns: callback - - callback call
| Param | Type | Description | | --- | --- | --- | | id | string | id from a enbedded Textfield into a form tag | | url | string | url string where to post the data rows | | callback | string | callback function, you need to declare a callbackup what will be called after the post |
Example
it will disable the form submit
var vwu = new Vanilla_website_utils();
let array= vwu.get_text_rows);module.exports#get_form_data(form) ⇒ array ⏏
Kind: Exported function
Returns: array - in key/value as JSON
| Param | Type | Description | | --- | --- | --- | | form | object | form object |
Example
var vwu = new Vanilla_website_utils();
let data = JSON.stringify(await vwu.get_form_data(form));module.exports#s_fill_select(select_obj, data) ⏏
Kind: Exported function
| Param | Type | Description | | --- | --- | --- | | select_obj | object | DOM object | | data | object | data array |
Example
var vwu = new Vanilla_website_utils();
let extra_sel = document.querySelector("#extra");
await vwu.fill_select(extra_sel, data);module.exports#fill_select(select_obj, data, selected, copy2clipboard, log) ⏏
Kind: Exported function
| Param | Type | Default | Description | | --- | --- | --- | --- | | select_obj | object | | DOM object | | data | object | | data array | | selected | string | | select element | | copy2clipboard | boolean | false | copy2clipboard true or false | | log | object | false | log object - experimental |
Example
var vwu = new Vanilla_website_utils();
const url = api + '/fields?table=av0_style&db=sl&server=232&f=list';
const data = JSON.parse(await aget_api(url));
let extra_sel = document.querySelector("#extra");
await vwu.fill_select(extra_sel, data);
await vwu.fill_select(sel,all_tags,'height: 500px; width: 230px;font-size: 9px',true, log);module.exports#pad(number) ⇒ string ⏏
Kind: Exported function
Returns: string - - data array
| Param | Type | Description | | --- | --- | --- | | number | int | number |
Example
var vwu = new Vanilla_website_utils();
let x = vwu.pad(5);module.exports#s_set_selected(select_obj, data) ⏏
Kind: Exported function
| Param | Type | Description | | --- | --- | --- | | select_obj | object | DOM object | | data | object | data array |
Example
var vwu = new Vanilla_website_utils();
let extra_sel = document.querySelector("#extra");
await vwu.fill_select(extra_sel, data);module.exports#set_selected() ⏏
Kind: Exported function
Example
var vwu = new Vanilla_website_utils();
let select = document.querySelector("#bom");
await vwu.set_selected(select,v['bom']);