# HG changeset patch # User Yaroslav Zhuravlev # Date 1529412226 -10800 # Node ID 8e2b3aadc3ceeea2d40ca77e75f2735e7f48e583 # Parent e90725c6ac6e22550e37720ec502d7d9a4b9a4d0 Added njs String object. diff -r e90725c6ac6e -r 8e2b3aadc3ce xml/en/docs/njs/njs_api.xml --- a/xml/en/docs/njs/njs_api.xml Tue Jun 19 15:43:46 2018 +0300 +++ b/xml/en/docs/njs/njs_api.xml Tue Jun 19 15:43:46 2018 +0300 @@ -24,6 +24,383 @@
+
+ + +There are two types of strings: +a Unicode string (default) and +a byte string. + + + +A Unicode string corresponds to an ECMAScript string +which contains Unicode characters. + + + +Byte strings contain a sequence of bytes. +They are used to serialize Unicode strings +to external data and deserialize from external sources. +For example, the toUTF8() method serializes +a Unicode string to a byte string using UTF8 encoding: + +>> '£'.toUTF8().toString('hex') +c2a3 /* C2 A3 is the UTF8 representation of 00A3 ('£') code point */ + +The toBytes() method serializes +a Unicode string with code points up to 255 into a byte string, +otherwise, null is returned: + +>> '£'.toBytes().toString('hex') +a3 /* a3 is a byte equal to 00A3 ('£') code point */ + +Only byte strings can be converted to different encodings. +For example, a string cannot be encoded to hex directly: + +>> 'αβγδ'.toString('base64') +TypeError: argument must be a byte string + at String.prototype.toString (native) + at main (native) + +To convert a Unicode string to hex, +first, it should be converted to a byte string and then to hex: + +>> 'αβγδ'.toUTF8().toString('base64') +zrHOss6zzrQ= + + + + +String.fromCodePoint(codePoint1[, ...[, +codePoint2]]) + +Returns a string from one or more Unicode code points. + +>> String.fromCodePoint(97, 98, 99, 100) +abcd + + + +String.prototype.concat(string1[, ..., +stringN]) + +Returns a string that contains the concatenation of specified +strings. + +>> "a".concat("b", "c") +abc + + + +String.prototype.endsWith(searchString[, +length]) + +Returns true if a string ends with the characters +of a specified string, otherwise false. +The optional length parameter is the the length of string. +If omitted, the default value is the length of the string. + +>> 'abc'.endsWith('abc') +true +>> 'abca'.endsWith('abc') +false + + + +String.prototype.fromBytes(start[, +end]) + +(njs specific) Returns a new Unicode string from a byte string +where each byte is replaced with a corresponding Unicode code point. + + +String.prototype.fromUTF8(start[, +end]) + +(njs specific) Converts a byte string containing a valid UTF8 string +into a Unicode string, +otherwise null is returned. + + +String.prototype.includes(searchString[, +position])) + +Returns true if a string is found within another string, +otherwise false. +The optional position parameter is the position +within the string at which to begin search for searchString. +Default value is 0. + +>> 'abc'.includes('bc') +true + + + +String.prototype.indexOf(searchString[, +fromIndex]) + +Returns the position of the first occurrence +of the searchString +The search is started at fromIndex. +Returns -1 if the value is not found. +The fromIndex is an integer, +default value is 0. +If fromIndex is lower than 0 +or greater than +String.prototype.length, +the search starts at index 0 and +String.prototype.length. + +>> 'abcdef'.indexOf('de', 2) +3 + + + +String.prototype.lastIndexOf(searchString[, +fromIndex]) + +Returns the position of the last occurrence +of the searchString, +searching backwards from fromIndex. +Returns -1 if the value is not found. +If searchString is empty, +then fromIndex is returned. + +>> "nginx".lastIndexOf("gi") +1 + + + +String.prototype.length + +Returns the length of the string. + +>> 'αβγδ'.length +4 + + + +String.prototype.match([regexp]) + +Matches a string against a regexp. + +>> 'nginx'.match( /ng/i ) +ng + + + +String.prototype.repeat(number) + +Returns a string +with the specified number of copies of the string. + +>> 'abc'.repeat(3) +abcabcabc + + + +String.prototype.replace([regexp|string[, +string|function]]) + +Returns a new string with matches of a pattern +(string or a regexp) +replaced by a string or a function. + +>> 'abcdefgh'.replace('d', 1) +abc1efgh + + + +String.prototype.search([regexp]) + +Searches for a string using a regexp + +>> 'abcdefgh'.search('def') +3 + + + +String.prototype.slice(start[, +end]) + +Returns a new string containing a part of an +original string between start +and end or +from start to the end of the string. + +>> 'abcdefghijklmno'.slice(NaN, 5) +abcde + + + +String.prototype.startsWith(searchString[, +position]) + +Returns true if a string begins with the characters +of a specified string, otherwise false. +The optional position parameter is the position +in this string at which to begin search for searchString. +Default value is 0. + +>> 'abc'.startsWith('abc') +true +> 'aabc'.startsWith('abc') +false + + + +String.prototype.substr(start[, +length]) + +Returns the part of the string of the specified length +from start +or from start to the end of the string. + +>> 'abcdefghijklmno'.substr(3, 5) +defgh + + + +String.prototype.substring(start[, +end]) + +Returns the part of the string between +start and end or +from start to the end of the string. + +>> 'abcdefghijklmno'.substring(3, 5) +de + + + +String.prototype.toBytes(start[, +end]) + +(njs specific) Serializes a Unicode string to a byte string. +Returns null if a character larger than 255 is +found in the string. + + +String.prototype.toLowerCase() + +Converts a string to lower case. +The method supports only simple Unicode folding. + +>> 'ΑΒΓΔ'.toLowerCase() +αβγδ + + + +String.prototype.toString([encoding]) + + +If no encoding is specified, +returns a specified Unicode string or byte string as in ECMAScript. + + + +(njs specific) If encoding is specified, +encodes a byte string to +hex, +base64, or +base64url. + + +>> 'αβγδ'.toUTF8().toString('base64url') +zrHOss6zzrQ + + + +String.prototype.toUpperCase() + +Converts a string to upper case. +The method supports only simple Unicode folding. + +>> 'αβγδ'.toUpperCase() +ΑΒΓΔ + + + +String.prototype.toUTF8(start[, +end]) + +(njs specific) Serializes a Unicode string +to a byte string using UTF8 encoding. + +>> 'αβγδ'.toUTF8().length +8 +>> 'αβγδ'.length +4 + + + +String.prototype.trim() + +Removes whitespaces from both ends of a string. + +>> ' abc '.trim() +abc + + + +String.prototype.split(([string|regexp[, +limit]])) + +Returns match of a string against a regexp. +The optional limit parameter is an integer that specifies +a limit on the number of splits to be found. + +>> 'abc'.split('') +a,b,c + + + +encodeURI(URI) + +encodes a URI by replacing each instance of certain characters by +one, two, three, or four escape sequences +representing the UTF-8 encoding of the character + +>> encodeURI('012αβγδ') +012%CE%B1%CE%B2%CE%B3%CE%B4 + + + +encodeURIComponent(encodedURIString) + +Encodes a URI by replacing each instance of certain characters +by one, two, three, or four escape sequences +representing the UTF-8 encoding of the character. + +>> encodeURIComponent('[@?=') +%5B%40%3F%3D + + + +decodeURI(encodedURI) + +Decodes a previously encoded URI. + +>> decodeURI('012%CE%B1%CE%B2%CE%B3%CE%B4') +012αβγδ + + + +decodeURIComponent(decodedURIString) + +Decodes an encoded component of a previously encoded URI. + +>> decodeURIComponent('%5B%40%3F%3D') +[@?= + + + + + + +
+ +