/**
* @file
* stax.js
* Systematic text analysis extension for JavaScript
* @version 0.3.4
* @license The MIT License (MIT)
*/
/**
* Stax
*
* @namespace
*/
const stax = {
/**
* Converts the first character to upper case
*
* @param {string} text - Input string
* @returns {string} String with the first character capitalized.
*/
capitalize: function(text) {
if (typeof text !== 'string') return '';
return text.charAt(0).toUpperCase() + text.slice(1);
},
/**
* Converts string into lower case
*
* @param {string} text - Input string
* @returns {string} Lower case string.
*/
casefold: function(text) {
return text.toLowerCase();
},
/**
* Returns a centered string
*
* @param {string} text - Input string
* @param {number} width - Total width to center the string within
* @returns {string} Centered string.
*/
center: function(text, width) {
const space = Math.max(width - text.length, 0);
const padLeft = Math.floor(space / 2);
const padRight = space - padLeft;
return ' '.repeat(padLeft) + text + ' '.repeat(padRight);
},
/**
* Returns the number of times a specified value occurs in a string
*
* @param {string} text - Input string
* @param {string} value - Value to count in the string
* @returns {number} Count of occurrences.
*/
count: function(text, value) {
return (text.match(new RegExp(value, 'g')) || []).length;
},
/**
* Returns true if the string ends with the specified value
*
* @param {string} text - Input string
* @param {string} suffix - Suffix to check
* @returns {boolean} True if the string ends with the specified value.
*/
endswith: function(text, suffix) {
return text.endsWith(suffix);
},
/**
* Sets the tab size of the string
*
* @param {string} text - Input string
* @param {number} tabsize - Number of spaces per tab
* @returns {string} String with expanded tabs.
*/
expandtabs: function(text, tabsize) {
return text.replace(/\t/g, ' '.repeat(tabsize));
},
/**
* Searches the string for a specified value and returns the position
* of where it was found.
*
* @param {string} text - Input string
* @param {string} value - Value to search for
* @returns {number} Position of the found value, or -1 if not found.
*/
find: function(text, value) {
return text.indexOf(value);
},
/**
* Formats specified values in a string
*
* @param {string} template - Template string with placeholders
* @param {...any} values - Values to replace placeholders
* @returns {string} Formatted string.
*/
format: function(template, ...values) {
return template.replace(/{(\d+)}/g, function(match, number) {
return typeof values[number] != 'undefined' ? values[number] : match;
});
},
/**
* Returns True if all characters in the string are alphanumeric
*
* @param {string} text - Input string
* @returns {boolean} True if the string is alphanumeric.
*/
isalnum: function(text) {
return /^[a-z0-9]+$/i.test(text);
},
/**
* Returns True if all characters in the string are alphabetic
*
* @param {string} text - Input string
* @returns {boolean} True if the string is alphabetic.
*/
isalpha: function(text) {
return /^[a-z]+$/i.test(text);
},
/**
* Returns True if all characters in the string are digits
*
* @param {string} text - Input string
* @returns {boolean} True if the string consists of digits.
*/
isdigit: function(text) {
return /^\d+$/.test(text);
},
/**
* Converts a string into lower case
*
* @param {string} text - Input string
* @returns {string} Lower case string.
*/
lower: function(text) {
return text.toLowerCase();
},
/**
* Returns a trimmed version of the string
*
* @param {string} text - Input string
* @returns {string} Trimmed string.
*/
strip: function(text) {
return text.trim();
},
/**
* Converts a string into upper case
*
* @param {string} text - Input string
* @returns {string} Upper case string.
*/
upper: function(text) {
return text.toUpperCase();
},
/**
* Fills the string with a specified number of 0 values at the beginning
*
* @param {string} text - Input string
* @param {number} width - Total width of the string after padding
* @returns {string} Padded string.
*/
zfill: function(text, width) {
return text.padStart(width, '0');
}
};
module.exports = stax;