Skip to content

Instantly share code, notes, and snippets.

@rauschma
Last active February 5, 2024 04:12
Show Gist options
  • Save rauschma/c46fc10f671ed5bf14021bc14f101c8d to your computer and use it in GitHub Desktop.
Save rauschma/c46fc10f671ed5bf14021bc14f101c8d to your computer and use it in GitHub Desktop.

Revisions

  1. rauschma revised this gist Apr 17, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion string.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ None of the string methods modify `this` – they always return fresh strings.

    * `charAt(pos: number): string` <sup>ES1</sup>

    Returns the character at index `pos`, as a string (JavaScript does not have a datatype for characters). `str[i]` is equivalent to `str.charAt(i)` and more concise.
    Returns the character at index `pos`, as a string (JavaScript does not have a datatype for characters). `str[i]` is equivalent to `str.charAt(i)` and more concise (caveat: may not work on old engines).

    ```repl
    > 'abc'.charAt(1)
  2. rauschma revised this gist Apr 17, 2018. 1 changed file with 34 additions and 7 deletions.
    41 changes: 34 additions & 7 deletions string.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,8 @@

    ## `String.prototype.*`

    None of the string methods modify `this` – they always return fresh strings.

    * `charAt(pos: number): string` <sup>ES1</sup>

    Returns the character at index `pos`, as a string (JavaScript does not have a datatype for characters). `str[i]` is equivalent to `str.charAt(i)` and more concise.
    @@ -281,19 +283,44 @@
    true
    ```

    * `substring(start: number, end=this.length): string` <sup>ES1</sup>

    Use `.slice()` instead of this method. `.substring()` wasn’t implemented consistently in older engines and doesn’t support negative indices.

    * `toUpperCase(): string` <sup>ES1</sup>

    Returns a copy of `this` in which all lowercase alphabetic characters are converted to uppercase. How well that works for various alphabets depends on the JavaScript engine.

    ```repl
    > '-a2b-'.toUpperCase()
    '-A2B-'
    > 'αβγ'.toUpperCase()
    'ΑΒΓ'
    ```

    * `toLowerCase(): string` <sup>ES1</sup>

    ## TODO
    Returns a copy of `this` in which all uppercase alphabetic characters are converted to lowercase. How well that works for various alphabets depends on the JavaScript engine.

    * `String.prototype.substring ( start, end )`
    * `String.prototype.toLowerCase ( )`
    * `String.prototype.toString ( )`
    * `String.prototype.toUpperCase ( )`
    * `String.prototype.trim ( )`
    ```repl
    > '-A2B-'.toLowerCase()
    '-a2b-'
    > 'ΑΒΓ'.toLowerCase()
    'αβγ'
    ```

    * `trim(): string` <sup>ES5</sup>

    Returns a copy of `this` in which all leading and trailing whitespace is removed.

    ```repl
    > '\r\n# \t'.trim()
    '#'
    ```

    ## More information

    * String methods of various ECMAScript versions in detail: http://exploringjs.com
    * Chapter “[RegExp named capture groups](http://exploringjs.com/es2018-es2019/ch_regexp-named-capture-groups.html)” in “Exploring ES2018 and ES2019”

    ## Sources

  3. rauschma revised this gist Apr 17, 2018. 1 changed file with 27 additions and 2 deletions.
    29 changes: 27 additions & 2 deletions string.md
    Original file line number Diff line number Diff line change
    @@ -254,11 +254,36 @@
    'bc'
    ```

    * `split(separator: string | RegExp, limit?: number): string[]` <sup>ES3</sup>

    Splits `this` into an Array of substrings – the strings that occur between the separators. The separator can either be a string or a regular expression. Captures made by groups in the regular expression are included in the result.

    ```repl
    > 'abc'.split('')
    [ 'a', 'b', 'c' ]
    > 'a | b | c'.split('|')
    [ 'a ', ' b ', ' c' ]
    > 'a | b | c'.split(/ *\| */)
    [ 'a', 'b', 'c' ]
    > 'a | b | c'.split(/( *)\|( *)/)
    [ 'a', ' ', ' ', 'b', ' ', ' ', 'c' ]
    ```

    * `startsWith(searchString: string, startPos=0): boolean` <sup>ES6</sup>

    Returns `true` if `this` starts with `searchString` at index `startPos` and `false`, otherwise.

    ```repl
    > '.gitignore'.startsWith('.')
    true
    > 'abc'.startsWith('bc', 1)
    true
    ```


    ## TODO

    * `String.prototype.split ( separator, limit )`
    * `String.prototype.startsWith ( searchString [ , position ] )`
    * `String.prototype.substring ( start, end )`
    * `String.prototype.toLowerCase ( )`
    * `String.prototype.toString ( )`
  4. rauschma revised this gist Apr 17, 2018. 1 changed file with 25 additions and 2 deletions.
    27 changes: 25 additions & 2 deletions string.md
    Original file line number Diff line number Diff line change
    @@ -230,10 +230,33 @@
    'a |04| b'
    ```

    * `search(regExp: string | RegExp): number` <sup>ES3</sup>

    Returns the index at which `regExp` occurs within `this`. If `regExp` is a string, it is converted to a regular expression.

    ```repl
    > 'a2b'.search(/[0-9]/)
    1
    > 'a2b'.search('[0-9]')
    1
    ```

    * `slice(start=0, end=this.length): string` <sup>ES3</sup>

    Returns the substring of `this` that starts at (including) index `start` and ends at (excluding) index `end`. You can use negative indices where `-1` means `this.length-1` (etc.).

    ```repl
    > 'abc'.slice(1, 3)
    'bc'
    > 'abc'.slice(1)
    'bc'
    > 'abc'.slice(-2)
    'bc'
    ```


    ## TODO

    * `String.prototype.search ( regexp )`
    * `String.prototype.slice ( start, end )`
    * `String.prototype.split ( separator, limit )`
    * `String.prototype.startsWith ( searchString [ , position ] )`
    * `String.prototype.substring ( start, end )`
  5. rauschma revised this gist Apr 16, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions string.md
    Original file line number Diff line number Diff line change
    @@ -97,7 +97,7 @@
    }
    ```

    Numbered capture groups become Array indices. Named capture groups (ES2018) become properties of `.groups`. In this mode, `.match()` works like `RegExp.prototype.exec()`.
    Numbered capture groups become Array indices. [Named capture groups](http://exploringjs.com/es2018-es2019/ch_regexp-named-capture-groups.html) (ES2018) become properties of `.groups`. In this mode, `.match()` works like `RegExp.prototype.exec()`.

    Examples:

    @@ -196,7 +196,7 @@
    'a |a | b'
    ```

    Named capture groups (ES2018) are supported, too:
    [Named capture groups](http://exploringjs.com/es2018-es2019/ch_regexp-named-capture-groups.html) (ES2018) are supported, too:

    * `$<name>` becomes the capture of named group `name`

    @@ -222,7 +222,7 @@
    'a |2018-04| b'
    ```

    Named capture groups (ES2018) are supported, too. If there are any, a last parameter contains an object whose properties contain the captures:
    [Named capture groups](http://exploringjs.com/es2018-es2019/ch_regexp-named-capture-groups.html) (ES2018) are supported, too. If there are any, a last parameter contains an object whose properties contain the captures:

    ```repl
    > const replacer = (...args) => { const groups=args.pop(); return '|'+groups.month+'|' };
  6. rauschma revised this gist Apr 16, 2018. 1 changed file with 114 additions and 18 deletions.
    132 changes: 114 additions & 18 deletions string.md
    Original file line number Diff line number Diff line change
    @@ -123,33 +123,129 @@
    null
    ```

    * `normalize(form: 'NFC'|'NFD'|'NFKC'|'NFKD' = 'NFC'): string;`
    * `normalize(form: 'NFC'|'NFD'|'NFKC'|'NFKD' = 'NFC'): string` <sup>ES6</sup>

    Normalizes `this` according to [the Unicode Normalization Forms](https://unicode.org/reports/tr15/).

    * `padEnd(len: number, fillString=' '): string` <sup>ES2017</sup>

    Appends `fillString` to `this` until it has the desired length `len`.

    ```repl
    > '#'.padEnd(2)
    '# '
    > 'abc'.padEnd(2)
    'abc'
    > '#'.padEnd(5, 'abc')
    '#abca'
    ```

    * `padStart(len: number, fillString=' '): string` <sup>ES2017</sup>

    Prepends `fillString` to `this` until it has the desired length `len`.

    ```repl
    > '#'.padStart(2)
    ' #'
    > 'abc'.padStart(2)
    'abc'
    > '#'.padStart(5, 'abc')
    'abca#'
    ```

    * `repeat(count=0): string` <sup>ES6</sup>

    Returns a string that is `this`, repeated `count` times.

    ```repl
    > '*'.repeat()
    ''
    > '*'.repeat(3)
    '***'
    ```

    * `replace(searchValue: string | RegExp, replaceValue: string): string` <sup>ES3</sup>

    Replace matches of `searchValue` with `replaceValue`. If `searchValue` is a string, only the first verbatim occurrence is replaced. If `searchValue` is a regular expression without flag `/g`, only the first match is replaced. If `searchValue` is a regular expression with `/g` then all matches are replaced.

    ```repl
    > 'x.x.'.replace('.', '#')
    'x#x.'
    > 'x.x.'.replace(/./, '#')
    '#.x.'
    > 'x.x.'.replace(/./g, '#')
    '####'
    ```

    Special characters in `replaceValue` are:

    * `$$`: becomes `$`
    * `$n`: becomes the capture of numbered group `n` (alas, `$0` does not work)
    * `$&`: becomes the complete match
    * `` $` ``: becomes everything before the match
    * `$'`: becomes everything after the match

    Examples:

    ```repl
    > 'a 2018-04 b'.replace(/([0-9]{4})-([0-9]{2})/, '|$2|')
    'a |04| b'
    > 'a 2018-04 b'.replace(/([0-9]{4})-([0-9]{2})/, '|$&|')
    'a |2018-04| b'
    > 'a 2018-04 b'.replace(/([0-9]{4})-([0-9]{2})/, '|$`|')
    'a |a | b'
    ```

    Named capture groups (ES2018) are supported, too:

    * `$<name>` becomes the capture of named group `name`

    Example:

    ```repl
    > 'a 2018-04 b'.replace(/(?<year>[0-9]{4})-(?<month>[0-9]{2})/, '|$<month>|')
    'a |04| b'
    ```


    * `replace(searchValue: string | RegExp, replacer: (substr: string, ...args: any[]) => string): string` <sup>ES3</sup>

    If the second parameter is a function occurrences are replaced with the strings it returns. Its parameters are:

    * `all`: the complete match
    * `g1`, `g2`, etc.: whatever was captured by numbered group 1, etc.
    * `offset`: where was the match found in the input string?
    * `string`: the whole input string

    ```repl
    > 'a 2018-04 b'.replace(/([0-9]{4})-([0-9]{2})/, (all, year, month) => '|'+all+'|')
    'a |2018-04| b'
    ```

    Named capture groups (ES2018) are supported, too. If there are any, a last parameter contains an object whose properties contain the captures:

    ```repl
    > const replacer = (...args) => { const groups=args.pop(); return '|'+groups.month+'|' };
    > 'a 2018-04 b'.replace(/(?<year>[0-9]{4})-(?<month>[0-9]{2})/, replacer)
    'a |04| b'
    ```

    ## TODO

    * String.prototype.padEnd( maxLength [ , fillString ] )
    * String.prototype.padStart( maxLength [ , fillString ] )
    * String.prototype.repeat ( count )
    * String.prototype.replace ( searchValue, replaceValue )
    * String.prototype.search ( regexp )
    * String.prototype.slice ( start, end )
    * String.prototype.split ( separator, limit )
    * String.prototype.startsWith ( searchString [ , position ] )
    * String.prototype.substring ( start, end )
    * String.prototype.toLocaleLowerCase ( [ reserved1 [ , reserved2 ] ] )
    * String.prototype.toLocaleUpperCase ( [ reserved1 [ , reserved2 ] ] )
    * String.prototype.toLowerCase ( )
    * String.prototype.toString ( )
    * String.prototype.toUpperCase ( )
    * String.prototype.trim ( )
    * String.prototype.valueOf ( )
    * String.prototype [ @@iterator ] ( )
    * `String.prototype.search ( regexp )`
    * `String.prototype.slice ( start, end )`
    * `String.prototype.split ( separator, limit )`
    * `String.prototype.startsWith ( searchString [ , position ] )`
    * `String.prototype.substring ( start, end )`
    * `String.prototype.toLowerCase ( )`
    * `String.prototype.toString ( )`
    * `String.prototype.toUpperCase ( )`
    * `String.prototype.trim ( )`

    ## More information

    * String methods of various ECMAScript versions in detail: http://exploringjs.com
    * Chapter “[RegExp named capture groups](http://exploringjs.com/es2018-es2019/ch_regexp-named-capture-groups.html)” in “Exploring ES2018 and ES2019”

    ## Sources

  7. rauschma revised this gist Apr 16, 2018. 1 changed file with 46 additions and 3 deletions.
    49 changes: 46 additions & 3 deletions string.md
    Original file line number Diff line number Diff line change
    @@ -81,11 +81,54 @@
    2
    ```

    * `match(regExp: string | RegExp): RegExpMatchArray | null` <sup>ES3</sup>

    If `regExp` is a regular expression with flag `/g` not set then `.match()` returns the first match for `regExp` within `this`. Or `null` if there is no match. If `regExp` is a string, it is converted to a regular expression before performing the previous steps.

    The result has the following type:

    ```js
    interface RegExpMatchArray extends Array<string> {
    index: number;
    input: string;
    groups: undefined | {
    [key: string]: string
    };
    }
    ```

    Numbered capture groups become Array indices. Named capture groups (ES2018) become properties of `.groups`. In this mode, `.match()` works like `RegExp.prototype.exec()`.

    Examples:

    ```repl
    > 'ababb'.match(/a(b+)/)
    [ 'ab', 'b', index: 0, input: 'ababb', groups: undefined ]
    > 'ababb'.match(/a(?<foo>b+)/)
    [ 'ab', 'b', index: 0, input: 'ababb', groups: { foo: 'b' } ]
    > 'abab'.match(/x/)
    null
    ```

    * `match(regExp: RegExp): string[] | null` <sup>ES3</sup>

    If flag `/g` of `regExp` is set, `.match()` returns either an Array with all matches or `null` if there was no match.

    ```repl
    > 'ababb'.match(/a(b+)/g)
    [ 'ab', 'abb' ]
    > 'ababb'.match(/a(?<foo>b+)/g)
    [ 'ab', 'abb' ]
    > 'abab'.match(/x/g)
    null
    ```

    * `normalize(form: 'NFC'|'NFD'|'NFKC'|'NFKD' = 'NFC'): string;`

    Normalizes `this` according to [the Unicode Normalization Forms](https://unicode.org/reports/tr15/).

    ## TODO

    * String.prototype.localeCompare ( that [ , reserved1 [ , reserved2 ] ] )
    * String.prototype.match ( regexp )
    * String.prototype.normalize ( [ form ] )
    * String.prototype.padEnd( maxLength [ , fillString ] )
    * String.prototype.padStart( maxLength [ , fillString ] )
    * String.prototype.repeat ( count )
  8. rauschma revised this gist Apr 16, 2018. 1 changed file with 28 additions and 4 deletions.
    32 changes: 28 additions & 4 deletions string.md
    Original file line number Diff line number Diff line change
    @@ -33,9 +33,9 @@
    'abcdefgh'
    ```

    * `endsWith(searchString: string, endPosition=this.length): boolean` <sup>ES6</sup>
    * `endsWith(searchString: string, endPos=this.length): boolean` <sup>ES6</sup>

    Returns `true` if `this` ends with `searchString` at index `endPosition` and `false`, otherwise.
    Returns `true` if `this` ends with `searchString` at index `endPos` and `false`, otherwise.

    ```repl
    > 'foo.txt'.endsWith('.txt')
    @@ -55,10 +55,34 @@
    false
    ```

    * `indexOf(searchString: string, minIndex=0): number` <sup>ES1</sup>

    Returns the lowest index at which `searchString` appears within `this`, or `-1`, otherwise. Any returned index will be `minIndex` or higher.

    ```repl
    > 'abab'.indexOf('a')
    0
    > 'abab'.indexOf('a', 1)
    2
    > 'abab'.indexOf('c')
    -1
    ```

    * `lastIndexOf(searchString: string, maxIndex=Infinity): number` <sup>ES1</sup>

    Returns the highest index at which `searchString` appears within `this`, or `-1`, otherwise. Any returned index will be `maxIndex` or lower.

    ```repl
    > 'abab'.lastIndexOf('ab', 2)
    2
    > 'abab'.lastIndexOf('ab', 1)
    0
    > 'abab'.lastIndexOf('ab')
    2
    ```

    ## TODO

    * String.prototype.indexOf ( searchString [ , position ] )
    * String.prototype.lastIndexOf ( searchString [ , position ] )
    * String.prototype.localeCompare ( that [ , reserved1 [ , reserved2 ] ] )
    * String.prototype.match ( regexp )
    * String.prototype.normalize ( [ form ] )
  9. rauschma created this gist Apr 16, 2018.
    91 changes: 91 additions & 0 deletions string.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    # Strings

    ## `String.prototype.*`

    * `charAt(pos: number): string` <sup>ES1</sup>

    Returns the character at index `pos`, as a string (JavaScript does not have a datatype for characters). `str[i]` is equivalent to `str.charAt(i)` and more concise.

    ```repl
    > 'abc'.charAt(1)
    'b'
    ```

    * `charCodeAt(pos: number): number` <sup>ES1</sup>

    Returns the 16-bit number (0–65535) of the UTF-16 code unit (character) at index `pos`.

    ```repl
    > 'abc'.charCodeAt(1)
    98
    ```

    * `codePointAt(pos: number): number | undefined` <sup>ES6</sup>

    Returns the number of the Unicode code point of the 1–2 characters at index `pos`. If there is no such index, it returns `undefined`.

    * `concat(...strings: string[]): string` <sup>ES3</sup>

    Returns the concatenation of `this` and `strings`. `'a'+'b'` is equivalent to `'a'.concat('b')` and more concise.

    ```repl
    > 'ab'.concat('cd', 'ef', 'gh')
    'abcdefgh'
    ```

    * `endsWith(searchString: string, endPosition=this.length): boolean` <sup>ES6</sup>

    Returns `true` if `this` ends with `searchString` at index `endPosition` and `false`, otherwise.

    ```repl
    > 'foo.txt'.endsWith('.txt')
    true
    > 'abc'.endsWith('ab', 2)
    true
    ```

    * `includes(searchString: string, startPos=0): boolean` <sup>ES6</sup>

    Returns `true` if `this` contains the `searchString` and `false`, otherwise. The search starts at `startPos`.

    ```repl
    > 'abc'.includes('b')
    true
    > 'abc'.includes('b', 2)
    false
    ```

    ## TODO

    * String.prototype.indexOf ( searchString [ , position ] )
    * String.prototype.lastIndexOf ( searchString [ , position ] )
    * String.prototype.localeCompare ( that [ , reserved1 [ , reserved2 ] ] )
    * String.prototype.match ( regexp )
    * String.prototype.normalize ( [ form ] )
    * String.prototype.padEnd( maxLength [ , fillString ] )
    * String.prototype.padStart( maxLength [ , fillString ] )
    * String.prototype.repeat ( count )
    * String.prototype.replace ( searchValue, replaceValue )
    * String.prototype.search ( regexp )
    * String.prototype.slice ( start, end )
    * String.prototype.split ( separator, limit )
    * String.prototype.startsWith ( searchString [ , position ] )
    * String.prototype.substring ( start, end )
    * String.prototype.toLocaleLowerCase ( [ reserved1 [ , reserved2 ] ] )
    * String.prototype.toLocaleUpperCase ( [ reserved1 [ , reserved2 ] ] )
    * String.prototype.toLowerCase ( )
    * String.prototype.toString ( )
    * String.prototype.toUpperCase ( )
    * String.prototype.trim ( )
    * String.prototype.valueOf ( )
    * String.prototype [ @@iterator ] ( )

    ## More information

    * String methods of various ECMAScript versions in detail: http://exploringjs.com

    ## Sources

    * https://github.com/Microsoft/TypeScript/blob/master/lib/lib.es6.d.ts
    * MDN
    * ECMAScript spec