What is string-kit?
string-kit is a versatile JavaScript library for string manipulation. It provides a wide range of utilities for working with strings, including formatting, padding, alignment, and more.
What are string-kit's main functionalities?
String Formatting
The `format` function allows you to format strings using placeholders, similar to printf in C. This is useful for creating dynamic strings based on variable values.
const string = require('string-kit');
console.log(string.format('%s is %d years old.', 'Alice', 30));
String Padding
The `pad` function pads a string to a specified length with a given character. This is useful for aligning text in console output or creating fixed-width string representations.
const string = require('string-kit');
console.log(string.pad('Hello', 10, ' '));
String Alignment
The `align` function aligns a string within a specified width, either left, right, or center. This is useful for formatting text in tables or other structured formats.
const string = require('string-kit');
console.log(string.align('Hello', 10, 'center', ' '));
String Truncation
The `truncate` function shortens a string to a specified length, optionally adding an ellipsis or other suffix. This is useful for displaying previews or summaries of longer text.
const string = require('string-kit');
console.log(string.truncate('This is a long string', 10));
Other packages similar to string-kit
string
The `string` package provides a wide range of string manipulation utilities, including trimming, padding, and formatting. It is similar to string-kit but offers a different API and some additional features.
lodash
Lodash is a utility library that provides a wide range of functions for working with arrays, objects, and strings. It includes string manipulation functions like padding, truncation, and case conversion. While it is more general-purpose than string-kit, it offers similar string manipulation capabilities.
underscore.string
underscore.string is a string manipulation library that extends Underscore.js with additional string functions. It provides utilities for trimming, padding, formatting, and more. It is similar to string-kit but designed to work seamlessly with Underscore.js.
String Kit
A string manipulation toolbox.
- License: MIT
- Current status: early alpha
- Platform: Node.js only (browser support is planned)
Install
Use Node Package Manager:
npm install string-kit
Full BDD spec generated by Mocha:
TOC
format()
should perform basic examples.
expect( format( 'Hello world' ) ).to.be( 'Hello world' ) ;
expect( format( 'Hello %s' , 'world' ) ).to.be( 'Hello world' ) ;
expect( format( 'Hello %s %s, how are you?' , 'Joe' , 'Doe' ) ).to.be( 'Hello Joe Doe, how are you?' ) ;
expect( format( 'I have %i cookies.' , 3 ) ).to.be( 'I have 3 cookies.' ) ;
%u should format unsigned integer.
expect( format( '%u' , 123 ) ).to.be( '123' ) ;
expect( format( '%u' , 0 ) ).to.be( '0' ) ;
expect( format( '%u' , -123 ) ).to.be( '0' ) ;
expect( format( '%u' ) ).to.be( '0' ) ;
%U should format positive unsigned integer.
expect( format( '%U' , 123 ) ).to.be( '123' ) ;
expect( format( '%U' , 0 ) ).to.be( '1' ) ;
expect( format( '%U' , -123 ) ).to.be( '1' ) ;
expect( format( '%U' ) ).to.be( '1' ) ;
should perform well the argument's number feature.
expect( format( '%s%s%s' , 'A' , 'B' , 'C' ) ).to.be( 'ABC' ) ;
expect( format( '%+1s%-1s%s' , 'A' , 'B' , 'C' ) ).to.be( 'BAC' ) ;
expect( format( '%3s%s' , 'A' , 'B' , 'C' ) ).to.be( 'CBC' ) ;
format.count() should count the number of arguments found.
expect( format.count( 'blah blih blah' ) ).to.be( 0 ) ;
expect( format.count( '%i %s' ) ).to.be( 2 ) ;
when using a filter object as the this context, the %[functionName] format should use a custom function to format the input.
var filters = {
fixed: function() { return 'F' ; } ,
double: function( str ) { return '' + str + str ; } ,
fxy: function( a , b ) { return '' + ( a * a + b ) ; }
} ;
expect( format.call( filters , '%[fixed]%s%s%s' , 'A' , 'B' , 'C' ) ).to.be( 'FABC' ) ;
expect( format.call( filters , '%s%[fxy:%a%a]' , 'f(x,y)=' , 5 , 3 ) ).to.be( 'f(x,y)=28' ) ;
expect( format.call( filters , '%s%[fxy:%+1a%-1a]' , 'f(x,y)=' , 5 , 3 ) ).to.be( 'f(x,y)=14' ) ;
Escape