lively.lang
Version:
JavaScript utils providing useful abstractions for working with collections, functions, objects.
437 lines (260 loc) • 9.54 kB
Markdown
String utility methods for printing, parsing, and converting strings.
- [d](
Takes a variable number of arguments. The first argument is the format
string. Placeholders in the format string are marked with `"%s"`.
```js
lively.lang.string.format("Hello %s!", "Lively User"); // => "Hello Lively User!"
```
```js
string.indent("Hello", " ", 2) // => " Hello"
```
Find out what the minum indentation of the text in str is
```js
minIndent(" Hello", " ") // => 2
```
#### <a name="changeIndent"></a>changeIndent(str, indentString, depth)
Add or remove indent from lines in str to match depth
```js
string.changeIndent(" Hello", " ", 1) // => " Hello"
```
```js
string.print("fo\"o") // => "\"fo\\\"o\""
```
Prints Arrays and escapes quotations. See `obj.inspect` for how to
completely print / inspect JavaScript data strcutures
```js
string.print([[1,2,3], "string", {foo: 23}])
// => [[1,2,3],"string",[object Object]]
```
```js
string.printNested([1,2,[3,4,5]]) // => "1\n2\n 3\n 4\n 5\n"
```
```js
pad("Foo", 2) // => "Foo "
pad("Foo", 2, true) // => " Foo"
```
#### <a name="printTable"></a>printTable(tableArray, options)
Takes a 2D Array and prints a table string. Kind of the reverse
operation to `tableize`
```js
string.printTable([["aaa", "b", "c"], ["d", "e","f"]])
// =>
// aaa b c
// d e f
```
A generic function to print a tree representation from a nested data structure.
Receives three arguments:
- `rootNode` an object representing the root node of the tree
- `nodePrinter` is a function that gets a tree node and should return stringified version of it
- `childGetter` is a function that gets a tree node and should return a list of child nodes
```js
var root = {name: "a", subs: [{name: "b", subs: [{name: "c"}]}, {name: "d"}]};
string.printTree(root, function(n) { return n.name; }, function(n) { return n.subs; });
// =>
// a
// |-b
// | \-c
// \-d
```
```js
string.toArray("fooo") // => ["f","o","o","o"]
```
```js
string.lines("foo\nbar\n\rbaz") // => ["foo","bar","baz"]
```
```js
var text = "Hello, this is a pretty long sentence\nthat even includes new lines."
+ "\n\n\nThis is a sentence in a new paragraph.";
string.paragraphs(text) // => [
// "Hello, this is a pretty long sentence\nthat even includes new lines.",
// "This is a sentence in a new paragraph."]
string.paragraphs(text, {keepEmptyLines: true}) // => [
// "Hello, this is a pretty long sentence\n that even includes new lines.",
// "\n ",
// "This is a sentence in a new paragraph."]
```
```js
string.nonEmptyLines("foo\n\nbar\n") // => ["foo","bar"]
```
```js
string.tokens(' a b c') => ['a', 'b', 'c']
```
Takes a String representing a "table" and parses it into a 2D-Array (as
accepted by the `collection.Grid` methods or `string.printTable`)
```js
options = {
convertTypes: BOOLEAN, // automatically convert to Numbers, Dates, ...?
cellSplitter: REGEXP // how to recognize "cells", by default just spaces
}
```
```js
string.tableize('a b c\nd e f')
// => [["a","b","c"],["d","e","f"]]
// can also parse csv like
var csv = '"Symbol","Name","LastSale",\n'
+ '"FLWS","1-800 FLOWERS.COM, Inc.","5.65",\n'
+ '"FCTY","1st Century Bancshares, Inc","5.65",'
string.tableize(csv, {cellSplitter: /^\s*"|","|",?\s*$/g})
// => [["Symbol","Name","LastSale"],
// ["FLWS","1-800 FLOWERS.COM, Inc.",5.65],
// ["FCTY","1st Century Bancshares, Inc",5.65]]
```
Converts [character entities](http://dev.w3.org/html5/html-author/charref)
into utf-8 strings
```js
string.unescapeCharacterEntities("foo && bar") // => "foo && bar"
```
```js
string.toQueryParams("http://example.com?foo=23&bar=test")
// => {bar: "test", foo: "23"}
```
Joins the strings passed as paramters together so that ea string is
connected via a single "/".
```js
string.joinPath("foo", "bar") // => "foo/bar";
```
```js
string.newUUID() // => "3B3E74D0-85EA-45F2-901C-23ECF3EAB9FB"
```
Takes some string representing content and a mime type.
For a list of mime types see: [http://www.iana.org/assignments/media-types/media-types.xhtml]()
More about data URIs: [https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs]()
```js
window.open(string.createDataURI('<h1>test</h1>', 'text/html'));
```
[]()
```js
string.hashCode("foo") // => 101574
```
© Joseph Myers [http://www.myersdaily.org/joseph/javascript/md5-text.html]()
```js
string.md5("foo") // => "acbd18db4cc2f85cedef654fccc4a4d8"
```
Different to the native `match` function this method returns an object
with `start`, `end`, and `match` fields
```js
string.reMatches("Hello World", /o/g)
// => [{start: 4, end: 5, match: "o"},{start: 7, end: 8, match: "o"}]
```
returns `{matched: true}` if success otherwise
`{matched: false, error: EXPLANATION, pattern: STRING|RE, pos: NUMBER}`
```js
string.stringMatch("foo 123 bar", "foo __/[0-9]+/__ bar") // => {matched: true}
string.stringMatch("foo aaa bar", "foo __/[0-9]+/__ bar")
// => {
// error: "foo <--UNMATCHED-->aaa bar",
// matched: false,
// pattern: /[0-9]+/,
// pos: 4
// }
```
Finds the next occurence of `needle` (String or RegExp). Returns delta
index.
```js
peekRight("Hello World", 0, /o/g) // => 4
peekRight("Hello World", 5, /o/) // => 2
```
#### <a name="peekLeft"></a>peekLeft(s, start, needle)
Similar to `peekRight`
For converting character positions to line numbers.
Returns a function accepting char positions. If the char pos is outside
of the line ranges -1 is returned.
```js
var idxComp = lineIndexComputer("Hello\nWorld\n\nfoo");
idxComp(3) // => 0 (index 3 is "l")
idxComp(6) // => 1 (index 6 is "W")
idxComp(12) // => 2 (index 12 is "\n")
```
For converting line numbers to [startIndex, endIndex]
```js
var idxComp = lineNumberToIndexesComputer("Hello\nWorld\n\nfoo");
idxComp(1) // => [6,12]
```
```js
endsWith("fooo!", "o!") // => true
```
#### <a name="withDecimalPrecision"></a>withDecimalPrecision(str, precision)
```js
withDecimalPrecision("1.12345678", 3) // => "1.123"
```
#### <a name="capitalize"></a>capitalize(s)
```js
capitalize("foo bar") // => "Foo bar"
```
#### <a name="camelCaseString"></a>camelCaseString(s)
Spaces to camels, including first char
```js
camelCaseString("foo bar baz") // => "FooBarBaz"
```
#### <a name="camelize"></a>camelize(s)
Dashes to camels, excluding first char
```js
camelize("foo-bar-baz") // => "fooBarBaz"
```
#### <a name="truncate"></a>truncate(s, length, truncation)
Enforces that s is not more then `length` characters long.
```js
truncate("123456789", 5) // => "12..."
```
#### <a name="truncateLeft"></a>truncateLeft(s, length, truncation)
Enforces that s is not more then `length` characters long.
```js
truncate("123456789", 5) // => "12..."
```
#### <a name="regExpEscape"></a>regExpEscape(s)
For creating RegExps from strings and not worrying about proper escaping
of RegExp special characters to literally match those.
```js
var re = new RegExp(regExpEscape("fooo{20}"));
re.test("fooo") // => false
re.test("fooo{20}") // => true
```
Uses char code.
```js
succ("a") // => "b"
succ("Z") // => "["
```
#### <a name="applyChange"></a>applyChange(string, change)
change is of the form
`{start: Number, end: Number, lines: [String], action: "insert"|"remove"}`
How many edit operations separate string a from b?
MIT licensed, https://gist.github.com/andrei-
Copyright (c) 2011 Andrei Mackenzie and https://github.com/kigiri
swap to save some memory O(min(a,b)) instead of O(a)
#### <a name="levenshtein"></a>levenshtein(a, b)
init the row
fill in the rest