jComponent / Helpers / String operations
Updated: 18. April 2019
Author: Peter Širka

String operations

Professional Support Chat with contributors

Here is the list of all registered string prototypes in jComponent library:


Method: String.arg()

+v17 Method replaces {key} for a obj[key].

String.arg(obj, [encode], [def]);
// @obj {Object} A model
// @encode {Boolean} optional, performs URL encode for values
// @def {String} optional, a default value (default: undefined)
// return {String}

console.log(('Hello {name}!').arg({ name: 'world' }));
// Hello world!

console.log(('Hello {missing}!').arg({ name: 'world' }));
// Hello {missing}!

console.log(('Hello {missing}!').arg({ name: 'world' }, 'MISSING'));
// Hello MISSING!

Method: String.COMPILABLE()

+v17 Method determines if the string contains some components, data-bind or something else for compilation. It's targeted for Tangular templates.

String.COMPILABLE();
// return {Boolean}

Method: String.env()

Method reads a value from environments.

String.env([search]);
// @search {Boolean} enables searching of values in the string
// return {String}

('[key]').env();                  // value 
('Lorem [key] ipsum').env();      // Lorem [key] ipsum 
('Lorem [key] ipsum').env(true);  // Lorem value ipsum 

Method: String.format()

Method formats the string.

String.format([a], [b], [n]);
// return {String}

('My name is {0}.').format('Peter');   // My name is Peter.
('{0} {1}').format('Hello', 'World');  // Hello World

Method: String.isEmail()

Method checks if the value is valid email address. Regullar expression for email validation is stored in: M.validators.email property.

String.isEmail();
// return {Boolean}

('your@domain.com').isEmail();  // true
('peter').isEmail();            // false

Method: String.isPhone()

Method checks if the value is valid phone (international) number. Regullar expression for phone validation is stored in: M.validators.phone property.

String.isPhone();
// return {Boolean}

('+421903163302').isPhone();  // true
('1234').isPhone();           // false

Method: String.isURL()

Method checks if the value is valid URL address. Regullar expression for URL validation is stored in: M.validators.url property.

String.isURL();
// return {Boolean}

('https://www.totaljs.com').isURL();  // true
('www.totaljs.com').isURL();          // false

Method: String.padLeft()

Method performs padding.

String.padLeft(count, [char]);
// @count {Number}
// @char {String} optional, default " "
// return {String}

Method: String.padRight()

Method performs padding.

String.padRight(count, [char]);
// @count {Number}
// @char {String} optional, default " "
// return {String}

Method: String.params()

Method peforms a simple formatting.

String.params(obj);
// @obj {Object}
// return {String}

('My name is {name}.').params({ name: 'Peter' });   // My name is Peter.

Method: String.parseConfig()

Method parses configuration from String.

String.parseConfig([def]. [callback]);
// @def {String/Object} Optional, default values
// @callback {Function(key, value)} Optional
// return {Object}

console.log('name:Peter;age:33;required:true'.parseConfig('length:30'));
// Output: { length: 30, name: "Peter", age: 33, required: true }

Method: String.parseDate()

Method parses date from String. It can parses time too.

String.parseDate();
// return {Date}

('1984-12-06').parseDate();            // Thu Dec 06 1984 00:00:00 GMT+0100 (CET)
('06.12.1984').parseDate();            // Thu Dec 06 1984 00:00:00 GMT+0100 (CET)
('06.12.1984 12:00:50').parseDate();   // Thu Dec 06 1984 12:00:50 GMT+0100 (CET)
('12:00').parseDate();                 // Current date 12:00:00 GMT+0100 (CET)
('10:00 PM').parseDate();              // +v17 Current date 22:00:00 GMT+0100 (CET)

Method: String.parseInt()

Method finds and parses the whole Number.

String.parseInt([def]);
// @def {Number} optional, default "0"
// return {Number}

('1984').parseInt();    // 1984
('19.84').parseInt();   // 19
('-1984').parseInt();   // -1984
('ABC').parseInt();     // 0
('ABC').parseInt(1);    // 1

Method: String.parseFloat()

Method finds and parses Float.

String.parseFloat([def]);
// @def {Number} optional, default "0"
// return {Number}

('19.84').parseFloat();   // 19.84

Method: String.removeDiacritics()

Method removes diacritics, basic latin + cyrillic.

String.removeDiacritics();
// return {String}

('ľščťžýáí').removeDiacritics();   // lsctzyai

Method: String.render()

Method performs Tangular.render().

String.render(model, [repository]);
// @model {Object}
// @repository {Object} optional
// return {String}

('My name is {{ name }}.').render({ name: 'Peter' });   // My name is Peter.

Method: String.SCOPE()

v17 Method returns a path according to the element scope.

String.SCOPE(element);
// @element {Component/Scope/jQuery/Element/Plugin} Important!!!
// return {String}

('?.name').SCOPE($('[data---]'));   // user.name

// self === jComponent
('?.name').SCOPE(self);   // user.name

Method: String.slug()

Method performs slugify.

String.slug(max);
// @max {Number} optional, default 60
// return {String}

('Peter Širka').slug();   // peter-sirka

Method: String.toSearch()

Method creates a search phrase.

String.toSearch();
// return {String}

('Banská Bystrica').toSearch();   // banska bistrica