Advertisement
Guest User

_.string global

a guest
Oct 10th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Type definitions for underscore.string
  2. // Project: https://github.com/epeli/underscore.string
  3. // Definitions by: Ry Racherbaumer <http://github.com/rygine>
  4. // Definitions: https://github.com/borisyankov/DefinitelyTyped
  5.  
  6. interface UnderscoreString {
  7.  
  8.     /**
  9.      * Determine if a string is 'blank.'
  10.      * @param str
  11.      */
  12.     isBlank(str: string): boolean;
  13.  
  14.     /**
  15.      * Removes all html tags from string.
  16.      * @param str
  17.      */
  18.     stripTags(str: string): string;
  19.  
  20.     /**
  21.      * Converts first letter of the string to uppercase.
  22.      * ('foo Bar') => 'Foo Bar'
  23.      * @param str
  24.      */
  25.     capitalize(str: string): string;
  26.  
  27.     /**
  28.      * Chop a string into pieces.
  29.      * ('whitespace', 3) => ['whi','tes','pac','e']
  30.      * @param str String to chop
  31.      * @param step Size of the pieces
  32.      */
  33.     chop(str: string, step: number): Array;
  34.  
  35.     /**
  36.      * Compress some whitespaces to one.
  37.      * (' foo bar ') => 'foo bar'
  38.      * @param str
  39.      */
  40.     clean(str: string): string;
  41.  
  42.     /**
  43.      * Count occurences of a sub string.
  44.      * ('Hello world', 'l') => 3
  45.      * @param str
  46.      * @param substr
  47.      */
  48.     count(str: string, substr: string): number;
  49.  
  50.     /**
  51.      * Convert string to an array of characters.
  52.      * ('Hello') => ['H','e','l','l','o']
  53.      * @param str
  54.      */
  55.     chars(str: string): Array;
  56.  
  57.     /**
  58.      * Returns a copy of the string in which all the case-based characters have had their case swapped.
  59.      * ('hELLO') => 'Hello'
  60.      * @param str
  61.      */
  62.     swapCase(str: string): string;
  63.  
  64.     /**
  65.      * Converts HTML special characters to their entity equivalents.
  66.      * ('<div>Blah blah blah</div>') => '&lt;div&gt;Blah blah blah&lt;/div&gt;'
  67.      * @param str
  68.      */
  69.     escapeHTML(str: string): string;
  70.  
  71.     /**
  72.      * Converts entity characters to HTML equivalents.
  73.      * ('&lt;div&gt;Blah blah blah&lt;/div&gt;') => '<div>Blah blah blah</div>'
  74.      * @param str
  75.      */
  76.     unescapeHTML(str: string): string;
  77.  
  78.     /**
  79.      * Escape a string for use in a regular expression.
  80.      * @param str
  81.      */
  82.     escapeRegExp(str: string): string;
  83.  
  84.     /**
  85.      * Splice a string like an array.
  86.      * @param str
  87.      * @param i
  88.      * @param howmany
  89.      * @param substr
  90.      */
  91.     splice(str: string, i: number, howmany: number, substr?: string): string;
  92.  
  93.     /**
  94.      * Insert a string at index.
  95.      * @param str
  96.      * @param i
  97.      * @param substr
  98.      */
  99.     insert(str: string, i: number, substr: string): string;
  100.  
  101.     /**
  102.      * Tests if string contains a substring.
  103.      * ('foobar', 'ob') => true
  104.      * @param str
  105.      * @param needle
  106.      */
  107.     include(str: string, needle: string): boolean;
  108.  
  109.     /**
  110.      * Tests if string contains a substring.
  111.      * ('foobar', 'ob') => true
  112.      * @param str
  113.      * @param needle
  114.      */
  115.     contains(str: string, needle: string): boolean;
  116.  
  117.     /**
  118.      * Joins strings together with given separator.
  119.      * (' ', 'foo', 'bar') => 'foo bar'
  120.      * @param separator
  121.      * @param args
  122.      */
  123.     join(separator: string, ...args: string[]): string;
  124.  
  125.     /**
  126.      * Split string by newlines character.
  127.      * ('Hello\nWorld') => ['Hello', 'World']
  128.      * @param str
  129.      */
  130.     lines(str: string): Array;
  131.  
  132.     /**
  133.      * Return reversed string.
  134.      * ('foobar') => 'raboof'
  135.      * @param str
  136.      */
  137.     reverse(str: string): string;
  138.  
  139.     /**
  140.      * Checks if string starts with another string.
  141.      * ('image.gif', 'image') => true
  142.      * @param str
  143.      * @param starts
  144.      */
  145.     startsWith(str: string, starts: string): boolean;
  146.  
  147.     /**
  148.      * Checks if string ends with another string.
  149.      * ('image.gif', 'gif') => true
  150.      * @param value
  151.      * @param starts
  152.      */
  153.     endsWith(value: string, starts: string): boolean;
  154.  
  155.     /**
  156.      * Returns the successor to passed string.
  157.      * ('a') => 'b'
  158.      * @param str
  159.      */
  160.     succ(str: string): string;
  161.  
  162.     /**
  163.      * Capitalize first letter of every word in the string.
  164.      * ('my name is epeli') => 'My Name Is Epeli'
  165.      * @param str
  166.      */
  167.     titleize(str: string): string;
  168.  
  169.     /**
  170.      * Converts underscored or dasherized string to a camelized one.
  171.      * ('-moz-transform') => 'MozTransform'
  172.      * @param str
  173.      */
  174.     camelize(str: string): string;
  175.  
  176.     /**
  177.      * Converts a camelized or dasherized string into an underscored one.
  178.      * ('MozTransform') => 'moz_transform'
  179.      * @param str
  180.      */
  181.     underscored(str: string): string;
  182.  
  183.     /**
  184.      * Converts a underscored or camelized string into an dasherized one.
  185.      * ('MozTransform') => '-moz-transform'
  186.      * @param str
  187.      */
  188.     dasherize(str: string): string;
  189.  
  190.     /**
  191.      * Converts string to camelized class name.
  192.      * ('some_class_name') => 'SomeClassName'
  193.      * @param str
  194.      */
  195.     classify(str: string): string;
  196.  
  197.     /**
  198.      * Converts an underscored, camelized, or dasherized string into a humanized one.
  199.      * Also removes beginning and ending whitespace, and removes the postfix '_id'.
  200.      * (' capitalize dash-CamelCase_underscore trim ') => 'Capitalize dash camel case underscore trim'
  201.      * @param str
  202.      */
  203.     humanize(str: string): string;
  204.  
  205.     /**
  206.      * Trims defined characters from begining and ending of the string.
  207.      * Defaults to whitespace characters.
  208.      * (' foobar ') => 'foobar'
  209.      * ('_-foobar-_', '_-') => 'foobar'
  210.      * @param str
  211.      * @param characters
  212.      */
  213.     trim(str: string, characters?: string): string;
  214.  
  215.     /**
  216.      * Trims defined characters from begining and ending of the string.
  217.      * Defaults to whitespace characters.
  218.      * (' foobar ') => 'foobar'
  219.      * ('_-foobar-_', '_-') => 'foobar'
  220.      * @param str
  221.      * @param characters
  222.      */
  223.     strip(str: string, characters?: string): string;
  224.  
  225.     /**
  226.      * Left trim. Similar to trim, but only for left side.
  227.      * @param str
  228.      * @param characters
  229.      */
  230.     ltrim(str: string, characters?: string): string;
  231.  
  232.     /**
  233.      * Left trim. Similar to trim, but only for left side.
  234.      * @param str
  235.      * @param characters
  236.      */
  237.     lstrip(str: string, characters?: string): string;
  238.  
  239.     /**
  240.      * Right trim. Similar to trim, but only for right side.
  241.      * @param str
  242.      * @param characters
  243.      */
  244.     rtrim(str: string, characters?: string): string;
  245.  
  246.     /**
  247.      * Right trim. Similar to trim, but only for right side.
  248.      * @param str
  249.      * @param characters
  250.      */
  251.     rstrip(str: string, characters?: string): string;
  252.  
  253.     /**
  254.      * Truncate string to specified length.
  255.      * ('Hello world').truncate(5) => 'Hello...'
  256.      * ('Hello').truncate(10) => 'Hello'
  257.      * @param str
  258.      * @param length
  259.      * @param truncateStr
  260.      */
  261.     truncate(str: string, length: number, truncateStr?: string): string;
  262.  
  263.     /**
  264.      * Elegant version of truncate.
  265.      * Makes sure the pruned string does not exceed the original length.
  266.      * Avoid half-chopped words when truncating.
  267.      * ('Hello, cruel world', 15) => 'Hello, cruel...'
  268.      * @param str
  269.      * @param length
  270.      * @param pruneStr
  271.      */
  272.     prune(str: string, length: number, pruneStr?: string): string;
  273.  
  274.     /**
  275.      * Split string by delimiter (String or RegExp).
  276.      * /\s+/ by default.
  277.      * (' I love you ') => ['I','love','you']
  278.      * ('I_love_you', '_') => ['I','love','you']
  279.      * @param str
  280.      * @param delimiter
  281.      */
  282.     words(str: string, delimiter?: string): Array;
  283.  
  284.     /**
  285.      * Pads a string with characters until the total string length is equal to the passed length parameter.
  286.      * By default, pads on the left with the space char (' ').
  287.      * padStr is truncated to a single character if necessary.
  288.      * ('1', 8) => ' 1'
  289.      * ('1', 8, '0') => '00000001'
  290.      * ('1', 8, '0', 'right') => '10000000'
  291.      * ('1', 8, '0', 'both') => '00001000'
  292.      * ('1', 8, 'bleepblorp', 'both') => 'bbbb1bbb'
  293.      * @param str
  294.      * @param length
  295.      * @param padStr
  296.      * @param type
  297.      */
  298.     pad(str: string, length: number, padStr:string, type?: string): string;
  299.  
  300.     /**
  301.      * Left-pad a string.
  302.      * Alias for pad(str, length, padStr, 'left')
  303.      * ('1', 8, '0') => '00000001'
  304.      * @param str
  305.      * @param length
  306.      * @param padStr
  307.      */
  308.     lpad(str: string, length: number, padStr?: string): string;
  309.  
  310.     /**
  311.      * Left-pad a string.
  312.      * Alias for pad(str, length, padStr, 'left')
  313.      * ('1', 8, '0') => '00000001'
  314.      * @param str
  315.      * @param length
  316.      * @param padStr
  317.      */
  318.     rjust(str: string, length: number, padStr?: string): string;
  319.  
  320.     /**
  321.      * Right-pad a string.
  322.      * Alias for pad(str, length, padStr, 'right')
  323.      * ('1', 8, '0') => '10000000'
  324.      * @param str
  325.      * @param length
  326.      * @param padStr
  327.      */
  328.     rpad(str: string, length: number, padStr?: string): string;
  329.  
  330.     /**
  331.      * Right-pad a string.
  332.      * Alias for pad(str, length, padStr, 'right')
  333.      * ('1', 8, '0') => '10000000'
  334.      * @param str
  335.      * @param length
  336.      * @param padStr
  337.      */
  338.     ljust(str: string, length: number, padStr?: string): string;
  339.  
  340.     /**
  341.      * Left/right-pad a string.
  342.      * Alias for pad(str, length, padStr, 'both')
  343.      * ('1', 8, '0') => '00001000'
  344.      * @param str
  345.      * @param length
  346.      * @param padStr
  347.      */
  348.     lrpad(str: string, length: number, padStr?: string): string;
  349.  
  350.     /**
  351.      * Left/right-pad a string.
  352.      * Alias for pad(str, length, padStr, 'both')
  353.      * ('1', 8, '0') => '00001000'
  354.      * @param str
  355.      * @param length
  356.      * @param padStr
  357.      */
  358.     center(str: string, length: number, padStr?: string): string;
  359.  
  360.     /**
  361.      * C like string formatting.
  362.      * _.sprintf('%.1f', 1.17) => '1.2'
  363.      * @param format
  364.      * @param args
  365.      */
  366.     sprintf(format: string, ...args: any[]): string;
  367.  
  368.     /**
  369.      * Parse string to number.
  370.      * Returns NaN if string can't be parsed to number.
  371.      * ('2.556').toNumber() => 3
  372.      * ('2.556').toNumber(1) => 2.6
  373.      * @param str
  374.      * @param decimals
  375.      */
  376.     toNumber(str: string, decimals?: number): number;
  377.  
  378.     /**
  379.      * Formats the numbers.
  380.      * (1000, 2) => '1,000.00'
  381.      * (123456789.123, 5, '.', ',') => '123,456,789.12300'
  382.      * @param number
  383.      * @param dec
  384.      * @param dsep
  385.      * @param tsep
  386.      */
  387.     numberFormat(number: number, dec?: number, dsep?: string, tsep?: string): string;
  388.  
  389.     /**
  390.      * Searches a string from left to right for a pattern.
  391.      * Returns a substring consisting of the characters in the string that are to the right of the pattern.
  392.      * If no match found, returns entire string.
  393.      * ('This_is_a_test_string').strRight('_') => 'is_a_test_string'
  394.      * @param str
  395.      * @param sep
  396.      */
  397.     strRight(str: string, sep: string): string;
  398.  
  399.     /**
  400.      * Searches a string from right to left for a pattern.
  401.      * Returns a substring consisting of the characters in the string that are to the right of the pattern.
  402.      * If no match found, returns entire string.
  403.      * ('This_is_a_test_string').strRightBack('_') => 'string'
  404.      * @param str
  405.      * @param sep
  406.      */
  407.     strRightBack(str: string, sep: string): string;
  408.  
  409.     /**
  410.      * Searches a string from left to right for a pattern.
  411.      * Returns a substring consisting of the characters in the string that are to the left of the pattern.
  412.      * If no match found, returns entire string.
  413.      * ('This_is_a_test_string').strLeft('_') => 'This'
  414.      * @param str
  415.      * @param sep
  416.      */
  417.     strLeft(str: string, sep: string): string;
  418.  
  419.     /**
  420.      * Searches a string from right to left for a pattern.
  421.      * Returns a substring consisting of the characters in the string that are to the left of the pattern.
  422.      * If no match found, returns entire string.
  423.      * ('This_is_a_test_string').strLeftBack('_') => 'This_is_a_test'
  424.      * @param str
  425.      * @param sep
  426.      */
  427.     strLeftBack(str: string, sep: string): string;
  428.  
  429.     /**
  430.      * Join an array into a human readable sentence.
  431.      * (['jQuery', 'Mootools', 'Prototype']) => 'jQuery, Mootools and Prototype'
  432.      * (['jQuery', 'Mootools', 'Prototype'], ', ', ' unt ') => 'jQuery, Mootools unt Prototype'
  433.      * @param array
  434.      * @param separator
  435.      * @param lastSeparator
  436.      * @param serial
  437.      */
  438.     toSentence(array: Array, separator?: string, lastSeparator?: string, serial?: boolean): string;
  439.  
  440.     /**
  441.      * The same as toSentence, but uses ', ' as default for lastSeparator.
  442.      * @param array
  443.      * @param separator
  444.      * @param lastSeparator
  445.      */
  446.     toSentenceSerial(array: Array, separator?: string, lastSeparator?: string): string;
  447.  
  448.     /**
  449.      * Transform text into a URL slug. Replaces whitespaces, accentuated, and special characters with a dash.
  450.      * ('Un éléphant à l'orée du bois') => 'un-elephant-a-loree-du-bois'
  451.      * @param str
  452.      */
  453.     slugify(str: string): string;
  454.  
  455.     /**
  456.      * Surround a string with another string.
  457.      * ('foo', 'ab') => 'abfooab'
  458.      * @param str
  459.      * @param wrapper
  460.      */
  461.     surround(str: string, wrapper: string): string;
  462.  
  463.     /**
  464.      * Quotes a string.
  465.      * quoteChar defaults to "
  466.      * ('foo') => '"foo"'
  467.      * @param str
  468.      */
  469.     quote(str: string, quoteChar?: string): string;
  470.  
  471.     /**
  472.      * Quotes a string.
  473.      * quoteChar defaults to "
  474.      * ('foo') => '"foo"'
  475.      * @param str
  476.      */
  477.     q(str: string, quoteChar?: string): string;
  478.  
  479.     /**
  480.      * Unquotes a string.
  481.      * quoteChar defaults to "
  482.      * ('"foo"') => 'foo'
  483.      * ("'foo'", "'") => 'foo'
  484.      * @param str
  485.      */
  486.     unquote(str: string, quoteChar?: string): string;
  487.  
  488.     /**
  489.      * Repeat a string with an optional separator.
  490.      * ('foo', 3) => 'foofoofoo'
  491.      * ('foo', 3, 'bar') => 'foobarfoobarfoo'
  492.      * @param value
  493.      * @param count
  494.      * @param separator
  495.      */
  496.     repeat(value: string, count: number, separator?:string): string;
  497.  
  498.     /**
  499.      * Naturally sort strings like humans would do.
  500.      * Caution: this is charset dependent.
  501.      * @param str1
  502.      * @param str2
  503.      */
  504.     naturalCmp(str1: string, str2: string): number;
  505.  
  506.     /**
  507.      * Calculates Levenshtein distance between two strings.
  508.      * ('kitten', 'kittah') => 2
  509.      * @param str1
  510.      * @param str2
  511.      */
  512.     levenshtein(str1: string, str2: string): number;
  513.  
  514.     /**
  515.      * Turn strings that can be commonly considered as booleans to real booleans.
  516.      * Such as "true", "false", "1" and "0". This is case insensitive.
  517.      * ('true') => true
  518.      * ('FALSE') => false
  519.      * ('random') => undefined
  520.      * ('truthy', ['truthy'], ['falsy']) => true
  521.      * ('true only at start', [/^true/]) => true
  522.      * @param str
  523.      * @param trueValues
  524.      * @param falseValues
  525.      */
  526.     toBoolean(str: string, trueValues?: Array, falseValues?: Array): boolean;
  527.  
  528. }
  529.  
  530. declare module _ {
  531.     export var string: UnderscoreString;
  532. }
  533.  
  534. _.string.isBlank("ggdsdfg");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement