Skip to content

WD JS Text Tools

wdonadelli edited this page Jun 5, 2022 · 24 revisions

WD JS Text Tools

To be considered as text type, the value argument must be a String that does not meet the criteria for defining date, time, number or null (A String without a printable character is considered a null value).

The initial value cannot be modified by methods or attributes.

To find the description of the tools presented in this section, it is recommended that you read the following topics:

camel

wd(text).camel
let a = wd("Stella maris");
a.camel; /* returns "stellaMaris" */
let b = wd("stella-maris");
b.camel; /* returns "Stella Maris" */

caps

wd(text).caps
let a = wd("Stella maris");
a.caps; /* returns "Stella Maris" */

clear

wd(text).clear
let a = wd("Acentuación prosódica y acentuación ortográfica");
a.clear; /* returns "Acentuacion prosodica y acentuacion ortografica" */

csv

wd(text).csv

The columns of the CSV file must be separated by the character \t and the lines by the character \n:

Cell00	Cell01	Cell02	Cell03
Cell10	Cell11	Cell12	Cell13
Cell20	Cell21	Cell22	Cell23
Cell30	Cell31	Cell32	Cell33
let a = wd("00\t01\t02\n10\t11\t12");
a.csv; /* returns [["00", "01", "02"], ["10", "11", "12"]] */

dash

wd(text).dash
let a = wd("Stella maris");
a.dash; /* returns "stella-maris" */
let b = wd("stellaMaris");
b.dash; /* returns "stella-maris" */

format

wd(text).format(attr...)
Argument Type Mandatory Description
attr String Yes Name of attributes to call formatting.
let a = wd("Stella maris");
a.format("dash", "upper"); /* returns "STELLA-MARIS" */
a.format("upper", "dash"); /* returns "s-t-e-l-l-a-m-a-r-i-s" */

If no attribute is given, returns null.

json

wd(text).json
let j = wd("{\"a\": 1, \"b\": \"unus\"}");
j.json; /* returns {a: 1, b: "unus"} */

lower

wd(text).lower
let a = wd("Stella maris");
a.lower; /* returns "stella maris" */

mask

wd(text).mask(check, callback)
Argument Type Mandatory Description
check String Yes Value to be checked by the mask.
callback Function No Extra validation function.

In case of success, it will return a String with the result of applying the mask, otherwise, null.

The mask encoding is entered as the textual content and has the following options:

Code RegExp Description
# [0-9] Numerical values.
@ [a-zA-ZÀ-ÿ] Latin alphabet.
* . Any character.
? None Mask separator.
let a = wd("(##) ####-####");
a.mask("4433339999");     /* returns "(44) 3333-9999" */
a.mask("(44) 3333-9999"); /* returns "(44) 3333-9999" */

The mask separator (?) has the function of testing several masks in sequence, if the first one informed does not match, it will test the second one and so on.

let a = wd("(##) ####-####?(##) # ####-####");
a.mask("4433339999");  /* returns "(44) 3333-9999" */
a.mask("44833339999"); /* returns "(44) 8 3333-9999" */
let b = wd("0.0#?0.##?#.##");
b.mask(1);   /* returns "0.01" */
b.mask(12);  /* returns "0.12" */
b.mask(123); /* returns "1.23" */

The extra check function will receive the matched mask as an argument and should return, depending on the analysis, true for mask validation or false when invalid.

function check(x) {
	let value = wd(x);
	return value.type === "date" ? true : false;
}

let a = wd("##/##/####");
a.mask("15042010", check); /* returns "15/04/2020" */
a.mask("04152010", check); /* returns null */

let b = wd("##/##/####?##.##.####");
a.mask("04152010", check); /* returns "04.15.2010" */

To use the encoding characters in the mask, you must insert two backslashes (\) before the character, except for the mask separator (?).

let a = wd("#\\# *@@@\\*");
a.mask("1$ABC"); /* returns "1# $ABC*" */

rpl

wd(text).rpl(search, change)
Argument Type Mandatory Description
search String Yes Value to find and replace.
change String Yes Substitute value.
let a = wd("Stella maris");
a.rpl("MARIS", "Maris"); /* returns "Stella maris" */
a.rpl("maris", "Maris"); /* returns "Stella Maris" */

tgl

wd(text).tgl
let a = wd("Stella maris");
a.tgl; /* returns "sTELLA MARIS" */

trim

wd(text).trim
let a = wd("  \t Stella   \t \n   maris \t ");
a.trim; /* returns "Stella maris" */

upper

wd(text).upper
let a = wd("Stella maris");
a.upper; /* returns "STELLA MARIS" */
Clone this wiki locally