@package/string
v0.0.11
Published
string manipulation functions
Downloads
3
Keywords
Readme
@package/string
string manipulation functions
Installation
>$ npm install @package/string
Test
@package/string>$ npm test
Build
if you want to convert @package/string into es5, you can simply run:
@package/string>$ npm run build
API
capitalize( item:String ):String
Returns a copy of the String with the first character in uppercase and all subsequent characters in lowercase.
Example:
import {capitalize} from '@package/string';
capitalize( 'LOREM IPSUM DOLOR' ) // returns => "Lorem ipsum dolor"
capitalize( 'lorem ipsum dolor' ) // returns => "Lorem ipsum dolor"
capitalize( 'Lorem Ipsum Dolor' ) // returns => "Lorem ipsum dolor"
capitalize( 'Lorem ipsum dolor' ) // returns => "Lorem ipsum dolor"
format( tpl:String, arg1:String[, arg2:String, ..., argN:String] ):String
Replaces the – zero indexed – numeric tokens in the String with the passed parameters.
If a token does not have a value, an empty String is used in its place.
NOTE: format
calls interpolate
internally.
Example:
import {format} from '@package/string';
format( '{0} {1} {2} {3}', 'lorem', 'ipsum', 'dolor' ) // returns => "lorem ipsum dolor "
guid():String
Generates a guid/uuid, the code for this was adapted from this gist.
import {guid} from '@package/string';
guid(); // returns something like => "286cb768-df10-4466-aabf-f5cb4ba406a2"
hyphenate( item:String ):String
Returns a copy of the String with any spaces and/ or underscores replaced by hyphens.
When uppercase characters are encountered they are also replaced with a hyphen followed by their lowercase equivelant.
Example:
import {hyphenate} from '@package/string';
hyphenate( 'Lorem IpsumDolor_sit' ) // returns => "lorem-ipsum-dolor-sit"
hyphenate( 'Lorem IPsumDolor_sit' ) // returns => "lorem-i-psum-dolor-sit"
hyphenate( 'Lorem IpsumDolor_-sit' ) // returns => "lorem-ipsum-dolor-sit"
id( item:Mixed[, prefix:String] ):String
Returns the id
property of the passed item – item can be an Object, HTMLElement, "JavaScript Class" instance, etc...
If an id
does not exist on the passed item
, the item is assigned an auto-generated id
and the value is returned.
If a prefix
is supplied then it is used as the prefix for the id
– if not anon
is used as the prefix
.
An internal counter that is automatically incremented is appended to the end of the prefix
and is separated from the prefix by a hyphen.
Example:
import {id} from '@package/string';
var foo = { id : 'foo' },
bar = { name : 'bar' },
yum = { nam : 'yum' };
id( foo ); // returns => "foo"
id( bar ); // returns => "anon-1000"
id( yum, 'yum' ); // returns => "yum-1001"
interpolate( tpl:String, dictionary:String[]|String{}[, pattern:RegExp] ):String
Replaces the tokens in the String with the values of the corresponding properties from the passed dictionary
Object.
Also accepts an optional second parameter allowing you to define your own token matching pattern
.
If a token does not have a value, an empty String is used in its place.
Example:
import {interpolate} from '@package/string';
interpolate( '{one} {two} {three} {four}', { one : 'lorem', two : 'ipsum', three : 'dolor' } ) // returns => "lorem ipsum dolor "
pad( num:Number, len:Number[, radix:Number] ):String
Returns a String representation of the passed Number instance with leading zeros. The String's minimum length will be equal to the passed len
parameter.
The method also accepts an optional radix
parameter. The default radix
is 10.
Example:
import {pad} from '@package/string';
pad( 16, 4 ); // returns => "0016"
pad( 16, 4, 8 ); // returns => "0020"
pad( 16, 4, 10 ); // returns => "0016"
pad( 16, 4, 16 ); // returns => "0010"
pad( 16, 4, 2 ); // returns => "10000"
toCamelCase( item:String ):String
Returns a copy of the String with any spaces and/ or hyphens removed and the lowercase character succeeding them transformed to uppercase.
Example:
import {toCamelCase} from '@package/string';
toCamelCase( 'Lorem IpsumDolor_sit' ) // returns => "LoremIpsumDolorSit"
toCamelCase( 'Lorem IPsumDolor_sit' ) // returns => "LoremIPsumDolorSit"
toCamelCase( 'lorem IpsumDolor_-sit' ) // returns => "loremIpsumDolorSit"
underscore( item:String ):String
Returns a copy of the String with any spaces and/ or hyphens replaced by underscores.
When uppercase characters are encountered they are also replaced with a underscore followed by their lowercase equivelant.
Example:
import {underscore} from '@package/string';
underscore( 'Lorem IpsumDolor_sit' ) // returns => "lorem_ipsum_dolor_sit"
underscore( 'Lorem IPsumDolor_sit' ) // returns => "lorem_i_psum_dolor_sit"
underscore( 'Lorem IpsumDolor_-sit' ) // returns => "lorem_ipsum_dolor_sit"
License
(The MIT License)
Copyright (c) 2011 christos "constantology" constandinou http://muigui.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.