0.0.3 • Published 10 years ago

kette v0.0.3

Weekly downloads
1
License
MIT
Repository
github
Last release
10 years ago

kette Travis CI Build Status Coverage Status

Collection of string manipulation functions for Node.js.

Usage

var kette = require('kette');

console.log( kette.leftSkip('Hello World', 3, 4) );
// outputs 'lo W'

Overview

Get parts
FunctionDescription
leftReturn characters from the left.
rightReturn characters from the right.
leftSkipReturn characters from the left, but skip some first.
rightSkipReturn characters from the right, but skip some first.
Add or remove somthing
FunctionDescription
wrapAdd to the left and right end.
unwrapRemove from the left and right end.
quoteAdd double quotes.
unquoteRemove double quotes.
padAdd leading zeros.
unpadRemove leading zeros.
repeatReturn multiple copies.
Format
FunctionDescription
reverseWell, return the reverse.
escapeRegexEscape all special chars.
Find substrings
FunctionDescription
containsTrue, if substring found.
startsWithTrue, if substring is at the left end.
endsWithTrue, if substring is at the right end.
allIndexesOfIndexes of all ocurrences of a substring.
allMatchesFind all regex matches and their indexes.
Arrays of strings
FunctionDescription
joinMerge an array of strings.
explodeSplit a string into an array of strings.
printlnPrint array elements on separate lines.
toStringsTurn all array elements into strings.
Transform
FunctionDescription
toCodePointsPut all character codes in an array.

Details

Get parts

left(str, count)
Returns from the string str the first count characters from the left.

var str = kette.left('Hello World', 3); // 'Hel'

right(str, count)
Returns from the string str the first count characters from the right.

var str = kette.right('Hello World', 3); // 'rld'

leftSkip(str, skip , count)
From left to right: Skips the first skip characters of str, then returns the next count characters. If countis omitted returns all characters up to the right end. If you skip more characters than there are in the string, the empty string is returned.

var str = kette.leftSkip('Hello World', 3, 4); // 'lo W'
var str = kette.leftSkip('Hello World', 5);    // ' World'
var str = kette.leftSkip('Hello World', 99);   // ''

rightSkip(str, skip , count)
From right to left: Skips the first skip characters of str, then returns the next count characters. If countis omitted returns all characters up to the left end. If you skip more characters than there are in the string, the empty string is returned.

var str = kette.rightSkip('Hello World', 3, 4); // 'o Wo'
var str = kette.rightSkip('Hello World', 5);    // 'Hello '
var str = kette.rightSkip('Hello World', 99);   // ''

Add or remove somthing

wrap(str1, str2)
Returns str2 + str1 + str2.

var str = kette.wrap('Hello World', '"'); // '"Hello World"'
var str = kette.wrap('Hello World', '--'); // '--Hello World--'

unwrap(str1 , str2)
If str1 starts and ends with str2, returns str1 with the left and right end occurrences of str2 removed.
If str1 does not start and end with str2, returns str1.
If str2 is omitted, returns str1 with the first and last characters removed, if they.are equal.

var str = kette.unwrap('--Hello World--', '--'); // 'Hello World'
var str = kette.unwrap('Hello World', '--');     // 'Hello World'
var str = kette.unwrap('xHello Worldy');         // 'xHello Worldy'

quote(str)
Add double quotes to the left end right ends.

var str = kette.quote('Hello World'); // '"Hello World"'

unquote(str)
Remove double quotes from the left end right ends.

var str = kette.unquote('"Hello World"'); // 'Hello World'

pad(strOrNum, len)
Add as many zeros to the left of strOrNum, such that the resulting string has at least length len. strOrNum may be a string or a number. If no additional zeros are needed to achieve len, the string is returned as it is.

var str = kette.pad(123, 6);      // '000123'
var str = kette.pad('789', 4);    // '0789'
var str = kette.pad('789055', 3); // '789055'

unpad(str)
Remove all leadings zeros from str.

var str = kette.unpad('000123');   // '123'
var str = kette.unpad('234567');   // '234567'

repeat(str, times)
Return a new string made out of times copies of str.

var str = kette.repeat('Ab', 3);   // 'AbAbAb'
var str = kette.repeat('xyz', 0);  // ''
var str = kette.repeat('cc', -99); // ''

Format

reverse(str)
Returns strwith all characters in reversed order.

var str = kette.reverse('Hello World!'); // '!dlroW olleH'

escapeRegex(str)
Returns a string equal to str but with all regular expression special characters properly escaped.

var str = kette.escapeRegex('[\\w]+'); // '\\[\\\\w\\]\\+'

Find substrings

contains(str1, str2)
Returns true, if str2 appears in str1.

var b = kette.contains('Hello my friend!', 'my');   // true
var b = kette.contains('Hello my friend!', 'dear'); // false

startsWith(str1, str2)
Returns true, if str1 starts with the prefix str2.

var b = kette.startsWith('Hello World', 'Hell'); // true

endsWith(str1, str2)
Returns true, if str1 ends with the suffix str2.

var b = kette.startsWith('Hello World', 'Hell'); // true

allIndexdesOf(str1, str2 , overlap)
Returns an array with the starting indexes of all occurrences of str2 in str1. The next occurrence is searched after the last occurrence has been found unless overlap is true. In that case, the next search begins at the next character.

var a = kette.allIndexdesOf('AbxxxAbxx', 'Ab');      // [0, 5]
var a = kette.allIndexdesOf('xxbbbbxx', 'bb');       // [2, 4]
var a = kette.allIndexdesOf('xxbbbbxx', 'bb', true); // [2, 3, 4]

allMatches(str, regex)
Returns an array with all matches of regex within str. The elements of the array are JSON objects with the following properties:

{
    'match':   // the substring of str that matches regex
    'from':    // starting index of the match
    'to':      // ending index of the match
    'pattern': // the regex pattern
}

The parameter regex is a string, which is internally used as new Regexp(regex, 'g').

var x = kette.allMatches('AbxxxAbxx', 'Ab');
// [ { match: 'Ab',
//     from:  0,
//     to:    1,
//     pattern: 'Ab' },
//   { match: 'Ab',
//     from:  5,
//     to:    6,
//     pattern: 'Ab' } ]

var x = kette.allMatches('xxbbbbxx', 'b+');
// [ { match: 'bbbb',
//     from:  2,
//     to:    5,
//     pattern: 'b+' } ]

Arrays of strings

join(array, separator)
Joins the elements of array into a single string using separator.

var str = kette.join(['My', 'dear', 'friend!'], ' '); // 'My dear friend!'
var str = kette.join(['123', '456', '789'], '');      // '123456789'

explode(str, separator , includeSep)
Splits the string str at all occurrences of separator into an array. If includeSep is true, the separator is also included in the array. Default is false.

var a = kette.explode('My dear friend!', ' ');       // ['My', 'dear', 'friend!']
var a = kette.explode('My dear friend!', ' ', true);
// ['My', ' ', 'dear', ' ', 'friend!']

println(array)
Prints all elements of array to the console (using console.log). Non-string elements are cast to string.

kette.println(['Hello', 5, 'World']);
'Hello'
'5'
'World'

toStrings(array)
Returns a new array with all elements of array cast to string.

var a = kette.toStrings(['Hello', 5, true]); // ['Hello', '5', 'true']

toCodePoints(str)
Returns an array with the character codes of all characters of str.

var a = kette.toCodePoints('Hello'); // [72, 101, 108, 108, 111]

License

The MIT License (MIT)

Copyright (c) 2014 Alexander Raasch

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.