string-kit-js
v1.3.0
Published
[](https://www.npmjs.com/package/string-kit-js) [](https://opensource.org/licenses/ISC)
Maintainers
Readme
string-kit-js
Simple string utility functions for JavaScript/Node.js
Current API groups
- Blank/empty checks:
isBlank,isEmpty,isNotBlank,isNotEmpty - Compare/null-safe:
equals,equalsIgnoreCase,compare - Search/index:
contains,indexOf,lastIndexOf,indexOfAny,lastIndexOfAny,indexOfAnyBut,lastIndexOfAnyBut - Character checks:
containsOnly,containsNone,containsAny,isAlpha,isNumeric,isWhitespace,isAsciiPrintable - Extract:
substring,left,right,mid,substringBefore,substringAfter,substringBetween,substringBeforeLast,substringAfterLast - Split/join:
split,join - Replace/remove:
remove,deleteSubstring,replace,overlay - Ending/prefix helpers:
appendIfMissing,prependIfMissing - Pad/repeat/layout:
leftPad,rightPad,center,repeat - Case/normalize:
toUpper,toLower,swapCase,capitalize,uncapitalize,normalizeSpace,stripAccents,toCamelCase,toKebabCase,toSnakeCase - Other transforms:
trim,trimToNull,trimToEmpty,chomp,chop,rotate,reverse,reverseDelimited,truncate,abbreviate - Defaults/diff/distance:
defaultIfBlank,defaultIfEmpty,defaultString,difference,levenshteinDistance,countMatches
Install
npm install string-kit-jsUsage
const {
isBlank,
isNotBlank,
isEmpty,
isNotEmpty,
equals,
equalsIgnoreCase,
contains,
startsWith,
endsWith,
toUpper,
toLower,
capitalize,
trim,
trimToNull,
trimToEmpty,
repeat,
reverse,
countMatches,
remove,
leftPad,
rightPad,
substringBetween,
normalizeSpace,
swapCase,
truncate,
abbreviate,
stripAccents,
toCamelCase,
toKebabCase,
toSnakeCase,
substringBeforeLast,
substringAfterLast,
...
} = require("string-kit-js");
// ===== Check blank =====
console.log("isBlank(null) ->", isBlank(null)); // true
console.log('isBlank(" ") ->', isBlank(" ")); // true
console.log('isNotBlank("abc") ->', isNotBlank("abc")); // true
// ===== Empty check =====
console.log('isEmpty("") ->', isEmpty("")); // true
console.log('isNotEmpty("abc") ->', isNotEmpty("abc")); // true
// ===== Equals =====
console.log('equals("a", "a") ->', equals("a", "a")); // true
console.log('equalsIgnoreCase("abc", "ABC") ->', equalsIgnoreCase("abc", "ABC")); // true
// ===== String contains =====
console.log('contains("hello world", "world") ->', contains("hello world", "world")); // true
console.log('startsWith("hello", "he") ->', startsWith("hello", "he")); // true
console.log('endsWith("hello", "lo") ->', endsWith("hello", "lo")); // true
// ===== Transform =====
console.log('toUpper("hello") ->', toUpper("hello")); // "HELLO"
console.log('toLower("WORLD") ->', toLower("WORLD")); // "world"
console.log('capitalize("hello") ->', capitalize("hello")); // "Hello"
// ===== Default value =====
console.log('defaultIfBlank(" ", "default") ->', defaultIfBlank(" ", "default")); // "default"
console.log('defaultIfEmpty("", "fallback") ->', defaultIfEmpty("", "fallback")); // "fallback"
// ===== Substring helpers =====
console.log('substringBefore("abc:def", ":") ->', substringBefore("abc:def", ":")); // "abc"
console.log('substringAfter("abc:def", ":") ->', substringAfter("abc:def", ":")); // "def"
// ===== Trim helpers =====
console.log('trim(" abc ") ->', trim(" abc ")); // "abc"
console.log('trimToNull(" ") ->', trimToNull(" ")); // null
console.log('trimToEmpty(null) ->', trimToEmpty(null)); // ""
// ===== Other helpers =====
console.log('repeat("ab", 3) ->', repeat("ab", 3)); // "ababab"
console.log('reverse("hello") ->', reverse("hello")); // "olleh"
console.log('countMatches("one one one", "one") ->', countMatches("one one one", "one")); // 3
console.log('remove("a-b-c", "-") ->', remove("a-b-c", "-")); // "abc"
console.log('leftPad("7", 3, "0") ->', leftPad("7", 3, "0")); // "007"
console.log('rightPad("7", 3, "0") ->', rightPad("7", 3, "0")); // "700"
console.log('substringBetween("[abc]", "[", "]") ->', substringBetween("[abc]", "[", "]")); // "abc"
console.log('substringBeforeLast("a/b/c", "/") ->', substringBeforeLast("a/b/c", "/")); // "a/b"
console.log('substringAfterLast("a/b/c", "/") ->', substringAfterLast("a/b/c", "/")); // "c"
// ===== Normalize & case convert =====
console.log('normalizeSpace(" a b c ") ->', normalizeSpace(" a b c ")); // "a b c"
console.log('swapCase("Hello JS") ->', swapCase("Hello JS")); // "hELLO js"
console.log('truncate("Hello world", 8) ->', truncate("Hello world", 8)); // "Hello..."
console.log('abbreviate("Hello world", 7) ->', abbreviate("Hello world", 7)); // "Hell..."
console.log('stripAccents("Tôi yêu JS") ->', stripAccents("Tôi yêu JS")); // "Toi yeu JS"
console.log('toCamelCase("hello world js") ->', toCamelCase("hello world js")); // "helloWorldJs"
console.log('toKebabCase("HelloWorld JS") ->', toKebabCase("HelloWorld JS")); // "hello-world-js"
console.log('toSnakeCase("HelloWorld JS") ->', toSnakeCase("HelloWorld JS")); // "hello_world_js"
