Lightweight string utility functions for JavaScript — type checking, transformation, URL helpers, and more.
npm install js_str_utilsOr add it as a dependency in your package.json:
"dependencies": {
"js_str_utils": "^1.4.7"
}const str = require('js_str_utils');
str.isString('hello'); // true
str.capitalize('hello world'); // 'Hello world'
str.truncate('Long text here', 10); // 'Long te...'
str.isPalindrome('madam'); // true
str.objToQueryStr({ page: 1, limit: 10 }); // '?page=1&limit=10'All functions are accessed via the single exported object:
const str = require('js_str_utils');Note: Every function throws
Error: Please provided a valid input!when passednull,undefined, or an empty string""— unless stated otherwise.
Returns true if the value is not null, not undefined, and not an empty string. Numbers are always valid. Throws for invalid input.
str.isValid('hello'); // true
str.isValid(0); // true
str.isValid(true); // true
str.isValid([]); // true
str.isValid(''); // throws Error
str.isValid(null); // throws ErrorReturns true if the value is null or undefined.
str.isNil(null); // true
str.isNil(undefined); // true
str.isNil('hello'); // false
str.isNil(0); // false
str.isNil(''); // throws ErrorReturns true if the value is exactly null.
str.isNull(null); // true
str.isNull(undefined); // throws Error
str.isNull('hello'); // false
str.isNull(''); // throws ErrorReturns true if the value is not null and not undefined.
str.isNotNil('hello'); // true
str.isNotNil(0); // true
str.isNotNil(null); // throws ErrorReturns true if the value is not null. Returns false for null or undefined.
str.isNotNull('hello'); // true
str.isNotNull(null); // false
str.isNotNull(undefined); // falseReturns true if the value is an object (including null and arrays). Returns false for primitives.
str.isObject({}); // true
str.isObject([1, 2]); // true
str.isObject(null); // true
str.isObject('hello'); // false
str.isObject(42); // falseReturns true if the value is a string type.
str.isString('hello'); // true
str.isString(''); // throws Error
str.isString(123); // false
str.isString(null); // throws ErrorReturns true if the value has a length property greater than 0. Works on strings and arrays.
str.isNotEmpty('hello'); // true
str.isNotEmpty([1]); // true
str.isNotEmpty([]); // false — length is 0Returns true if the value is a string and has length greater than 0.
str.isNotEmptyString('hi'); // true
str.isNotEmptyString(123); // false — not a stringReturns true if the value is not null, not undefined, and is a non-empty string.
str.isNotNilOrEmptyString('hi'); // true
str.isNotNilOrEmptyString(null); // false
str.isNotNilOrEmptyString(undefined); // falseReturns true if the value is of type number. Returns false for null, undefined, and empty string without throwing.
str.isNumber(42); // true
str.isNumber(0); // true
str.isNumber('42'); // false
str.isNumber(null); // false
str.isNumber(undefined); // falseReturns true if the value is not a number.
str.isNotNumber('hello'); // true
str.isNotNumber(42); // falseReturns true if the object has the given own property (does not include inherited/prototype properties).
str.hasProperty({ name: 'Alice' }, 'name'); // true
str.hasProperty({ name: 'Alice' }, 'age'); // false
const obj = Object.create({ inherited: true });
str.hasProperty(obj, 'inherited'); // false — inherited, not ownReturns true if the string or number reads the same forwards and backwards. Returns false for objects and arrays.
str.isPalindrome('madam'); // true
str.isPalindrome('racecar'); // true
str.isPalindrome(121); // true
str.isPalindrome('hello'); // false
str.isPalindrome(1234); // false
str.isPalindrome({}); // falseUppercases the first character of a string. The rest of the string is left unchanged.
str.capitalize('hello'); // 'Hello'
str.capitalize('javaScript'); // 'JavaScript'
str.capitalize('HELLO'); // 'HELLO'Converts every word's first character to uppercase and the rest to lowercase.
str.toTitleCase('hello world'); // 'Hello World'
str.toTitleCase('the QUICK brown FOX'); // 'The Quick Brown Fox'Clips a string to maxLength characters. If the string is longer, appends suffix (defaults to '...'). The returned string is always exactly maxLength characters.
| Parameter | Type | Default | Description |
|---|---|---|---|
str |
string |
— | The string to truncate |
maxLength |
number |
— | Maximum character count of the result |
suffix |
string |
'...' |
Appended when the string is clipped |
str.truncate('Hello, World!', 8); // 'Hello...' (8 chars)
str.truncate('Hello, World!', 8, '~'); // 'Hello, ~' (8 chars)
str.truncate('Hi', 10); // 'Hi' (unchanged, within limit)Removes whitespace from both ends of a string.
str.trim(' hello '); // 'hello'
str.trim('\t hi \n'); // 'hi'Removes whitespace from the start of a string only.
str.trimStart(' hello '); // 'hello 'Removes whitespace from the end of a string only.
str.trimEnd(' hello '); // ' hello'Reverses the characters in a string.
str.reverseString('hello'); // 'olleh'
str.reverseString('Hello World'); // 'dlroW olleH'
str.reverseString('madam'); // 'madam'Returns a new string consisting of str repeated n times.
str.repeat('ha', 3); // 'hahaha'
str.repeat('*', 5); // '*****'
str.repeat('abc', 0); // ''Removes all HTML tags from a string, leaving only the plain text content.
str.stripHtml('<p>Hello <b>World</b></p>'); // 'Hello World'
str.stripHtml('<a href="/path">Click here</a>'); // 'Click here'
str.stripHtml('No tags here'); // 'No tags here'Returns true if str begins with prefix. Case-sensitive.
str.startsWith('Hello World', 'Hello'); // true
str.startsWith('Hello World', 'World'); // false
str.startsWith('Hello World', 'hello'); // false — case-sensitiveReturns true if str ends with suffix. Case-sensitive.
str.endsWith('Hello World', 'World'); // true
str.endsWith('Hello World', 'Hello'); // false
str.endsWith('Hello World', 'world'); // false — case-sensitiveReturns true if str contains substr anywhere. Case-sensitive.
str.contains('Hello World', 'World'); // true
str.contains('Hello World', 'xyz'); // false
str.contains('JavaScript', 'Script'); // trueReturns the number of words in a string. Handles multiple spaces and leading/trailing whitespace.
str.countWords('Hello World'); // 2
str.countWords('The quick brown fox'); // 4
str.countWords(' extra spaces '); // 2Returns the number of non-overlapping occurrences of substr within str. Returns 0 for an empty substr.
str.countOccurrences('banana', 'an'); // 2
str.countOccurrences('hello world', 'world'); // 1
str.countOccurrences('hello', 'xyz'); // 0
str.countOccurrences('aaa', 'aa'); // 1 — non-overlapping
str.countOccurrences('hello', ''); // 0Converts a plain object to a URL query string.
str.objToQueryStr({ page: 1, limit: 10 }); // '?page=1&limit=10'
str.objToQueryStr({ q: 'node js' }); // '?q=node js'
str.objToQueryStr(null); // ''Parses query parameters from a URL string into a plain key/value object.
str.readParams('https://api.example.com/users?page=1&limit=10');
// { page: '1', limit: '10' }
str.readParams('page=2&sort=asc');
// { page: '2', sort: 'asc' }Returns the value of a single query parameter from a URL string. Returns undefined if the key is not found.
str.getParam('https://api.example.com?page=1&limit=10', 'page'); // '1'
str.getParam('https://api.example.com?page=1&limit=10', 'sort'); // undefinedReturns a native URLSearchParams instance, giving you full access to its built-in methods.
const params = str.urlSearch('page=1&sort=asc&sort=desc');
params.get('page'); // '1'
params.getAll('sort'); // ['asc', 'desc']
params.has('page'); // true
params.set('page', '2');
params.toString(); // 'page=2&sort=asc&sort=desc'
params.delete('sort');
params.append('filter', 'active');Encodes a URI string by escaping characters that are not allowed in a URL.
str.encodeURI('https://example.com/search?q=hello world');
// 'https://example.com/search?q=hello%20world'Decodes a previously encoded URI string back to its original form.
str.decodeURI('https://example.com/search?q=hello%20world');
// 'https://example.com/search?q=hello world'Functions that accept a string argument throw a standard error for invalid inputs:
try {
str.isString('');
} catch (e) {
console.error(e.message); // 'Please provided a valid input!'
}Inputs that always throw: "" (empty string), null, undefined.
Inputs that never throw (return false instead): isNumber, isNotNull, isNotNil — these handle null/undefined gracefully for convenience in conditional checks.
- Fork the repository and create a feature branch.
- Add your function to
src/index.js. - Add a corresponding spec file to
test/. - Ensure all tests pass:
npm test - Open a pull request against
main.
See DEVELOPER_GUIDE.md for detailed contribution instructions.