From 146b06c68fe791df1a95298d7f79e3cf0205d06b Mon Sep 17 00:00:00 2001 From: Bas Pennings Date: Tue, 15 Sep 2015 15:35:52 +0200 Subject: [PATCH] New definition files for string.js --- string/string-tests.ts | 286 +++++++++++++++++++++++++++++++++++++++++ string/string.d.ts | 134 +++++++++++++++++++ 2 files changed, 420 insertions(+) create mode 100644 string/string-tests.ts create mode 100644 string/string.d.ts diff --git a/string/string-tests.ts b/string/string-tests.ts new file mode 100644 index 000000000..22fca046d --- /dev/null +++ b/string/string-tests.ts @@ -0,0 +1,286 @@ +/// + +import S = require('string'); + +S('hello').s //"hello" +S(['a,b']).s //"a,b" +S({hi: 'jp'}).s //"[object Object]"" + +S('foo').between('', '').s // => 'foo' +S('foo').between('', '').s // => 'foo' +S('foo').between('', '').s // => 'foo' +S('foo').between('', '').s // => '' +S('Some strings } are very {weird}, dont you think?').between('{', '}').s // => 'weird' +S('This is a test string').between('test').s // => ' string' +S('This is a test string').between('', 'test').s // => 'This is a ' + +S('data_rate').camelize().s; //'dataRate' +S('background-color').camelize().s; //'backgroundColor' +S('-moz-something').camelize().s; //'MozSomething' +S('_car_speed_').camelize().s; //'CarSpeed' +S('yes_we_can').camelize().s; //'yesWeCan' + +S('jon').capitalize().s; //'Jon' +S('JP').capitalize().s; //'Jp' + +S('foobar').chompLeft('foo').s; //'bar' +S('foobar').chompLeft('bar').s; //'foobar' + +S('foobar').chompRight('bar').s; //'foo' +S('foobar').chompRight('foo').s; //'foobar' + +var str = S(' String \t libraries are \n\n\t fun\n! ').collapseWhitespace().s; //'String libraries are fun !' + +S('JavaScript is one of the best languages!').contains('one'); //true + +S('JP likes to program. JP does not play in the NBA.').count("JP")// 2 +S('Does not exist.').count("Flying Spaghetti Monster") //0 +S('Does not exist.').count("Bigfoot") //0 +S('JavaScript is fun, therefore Node.js is fun').count("fun") //2 +S('funfunfun').count("fun") //3 + +S('dataRate').dasherize().s; //'data-rate' +S('CarSpeed').dasherize().s; //'-car-speed' +S('yesWeCan').dasherize().s; //'yes-we-can' +S('backgroundColor').dasherize().s; //'background-color' + +S('Ken Thompson & Dennis Ritchie').decodeHTMLEntities().s; //'Ken Thompson & Dennis Ritchie' +S('3 < 4').decodeHTMLEntities().s; //'3 < 4' + +S("hello jon").endsWith('jon'); //true + +S('
hi
').escapeHTML().s; //<div>hi</div> + +S('subdir').ensureLeft('/').s; //'/subdir' +S('/subdir').ensureLeft('/').s; //'/subdir' + +S('dir').ensureRight('/').s; //'dir/' +S('dir/').ensureRight('/').s; //'dir/' + +S('the_humanize_string_method').humanize().s //'The humanize string method' +S('ThehumanizeStringMethod').humanize().s //'Thehumanize string method' +S('the humanize string method').humanize().s //'The humanize string method' +S('the humanize_id string method_id').humanize().s //'The humanize id string method' +S('the humanize string method ').humanize().s //'The humanize string method' +S(' capitalize dash-CamelCase_underscore trim ').humanize().s //'Capitalize dash camel case underscore trim' + +S('JavaScript is one of the best languages!').include('one'); //true + +S("afaf").isAlpha(); //true +S('fdafaf3').isAlpha(); //false +S('dfdf--dfd').isAlpha(); //false + +S("afaf35353afaf").isAlphaNumeric(); //true +S("FFFF99fff").isAlphaNumeric(); //true +S("99").isAlphaNumeric(); //true +S("afff").isAlphaNumeric(); //true +S("Infinity").isAlphaNumeric(); //true +S("-Infinity").isAlphaNumeric(); //false +S("-33").isAlphaNumeric(); //false +S("aaff..").isAlphaNumeric(); //false + +S(' ').isEmpty(); //true +S('\t\t\t ').isEmpty(); //true +S('\n\n ').isEmpty(); //true +S('helo').isEmpty(); //false +S(null).isEmpty(); //true +S(undefined).isEmpty(); //true + +S('a').isLower(); //true +S('z').isLower(); //true +S('B').isLower(); //false +S('hijp').isLower(); //true +S('hi jp').isLower(); //false +S('HelLO').isLower(); //false + +S("3").isNumeric(); //true +S("34.22").isNumeric(); //false +S("-22.33").isNumeric(); //false +S("NaN").isNumeric(); //false +S("Infinity").isNumeric(); //false +S("-Infinity").isNumeric(); //false +S("JP").isNumeric(); //false +S("-5").isNumeric(); //false +S("000992424242").isNumeric(); //true + +S('a').isUpper() //false +S('z').isUpper() //false +S('B').isUpper() //true +S('HIJP').isUpper() //true +S('HI JP').isUpper() //false +S('HelLO').isUpper() //true +S('crème brûlée').latinise().s // 'creme brulee' + +S('My name is JP').left(2).s; //'My' +S('Hi').left(0).s; //'' +S('My name is JP').left(-2).s; //'JP', same as right(2) + +var stuff = "My name is JP\nJavaScript is my fav language\r\nWhat is your fav language?" +var lines = S(stuff).lines() +console.dir(lines) +/* +[ 'My name is JP', + 'JavaScript is my fav language', + 'What is your fav language?' ] +*/ + +S('hello').pad(5).s //'hello' +S('hello').pad(10).s //' hello ' +S('hey').pad(7).s //' hey ' +S('hey').pad(5).s //' hey ' +S('hey').pad(4).s //' hey' +S('hey').pad(7, '-').s//'--hey--' + +S('hello').padLeft(5).s //'hello' +S('hello').padLeft(10).s //' hello' +S('hello').padLeft(7).s //' hello' +S('hello').padLeft(6).s //' hello' +S('hello').padLeft(10, '.').s //'.....hello' + +S('hello').padRight(5).s //'hello' +S('hello').padRight(10).s //'hello ' +S('hello').padRight(7).s //'hello ' +S('hello').padRight(6).s //'hello ' +S('hello').padRight(10, '.').s //'hello.....' + +S("'a','b','c'").parseCSV(',', "'") //['a', 'b', 'c']) +S('"a","b","c"').parseCSV() // ['a', 'b', 'c']) +S('a,b,c').parseCSV(',', null) //['a', 'b', 'c']) +S("'a,','b','c'").parseCSV(',', "'") //['a,', 'b', 'c']) +S('"a","b",4,"c"').parseCSV(',', null) //['"a"', '"b"', '4', '"c"']) +S('"a","b","4","c"').parseCSV() //['a', 'b', '4', 'c']) +S('"a","b", "4","c"').parseCSV() //['a', 'b', '4', 'c']) +S('"a","b", 4,"c"').parseCSV(",", null) //[ '"a"', '"b"', ' 4', '"c"' ]) +S('"a","b\\"","d","c"').parseCSV() //['a', 'b"', 'd', 'c']) +S('"a","b\\"","d","c"').parseCSV() //['a', 'b"', 'd', 'c']) +S('"a\na","b","c"\n"a", """b\nb", "a"').parseCSV(',', '"', '"', '\n'); // [ [ 'a\na', 'b', 'c' ], [ 'a', '"b\nb', 'a' ] ] + +S(' ').repeat(5).s; //' ' +S('*').repeat(3).s; //'***' + +S(' does IT work? ').replaceAll(' ', '_').s; //'_does_IT_work?_' +S('Yes it does!').replaceAll(' ', '').s; //'Yesitdoes!' + +S(' 1 2 3--__--4 5 6-7__8__9--0').strip(' ', '_', '-').s; //'1234567890' +S('can words also be stripped out?').strip('words', 'also', 'be').s; //'can stripped out?' + +S('I AM CRAZY').right(2).s; //'ZY' +S('Does it work? ').right(4).s; //'k? ' +S('Hi').right(0).s; //'' +S('My name is JP').right(-2).s; //'My', same as left(2) + +S('my name is JP.').capitalize().s; //My name is JP. +var a = "Hello " + S('joe!'); //a = "Hello joe!" +S("Hello").toString() === S("Hello").s; //true + +var myString = S('War'); +myString.setValue('Peace').s; // 'Peace' + +S('Global Thermonuclear Warfare').slugify().s // 'global-thermonuclear-warfare' +S('Crème brûlée').slugify().s // 'creme-brulee' + +S("JP is a software engineer").startsWith("JP"); //true +S('wants to change the world').startsWith("politicians"); //false + +S('My, st[ring] *full* of %punct)').stripPunctuation().s; //My string full of punct + +S('

just some text

').stripTags().s //'just some text' +S('

just some text

').stripTags('p').s //'just some text' + +var str = "Hello {{name}}! How are you doing during the year of {{date-year}}?" +var values = {name: 'JP', 'date-year': 2013} +console.log(S(str).template(values).s) //'Hello JP! How are you doing during the year of 2013?' + +str = "Hello #{name}! How are you doing during the year of #{date-year}?" +console.log(S(str).template(values, '#{', '}').s) //'Hello JP! How are you doing during the year of 2013?' + +S.TMPL_OPEN = '{' +S.TMPL_CLOSE = '}' +str = "Hello {name}! How are you doing during the year of {date-year}?" +console.log(S(str).template(values).s) //'Hello JP! How are you doing during the year of 2013?' + +S(' ').times(5).s //' ' +S('*').times(3).s //'***' + +S('true').toBoolean() //true +S('false').toBoolean() //false +S('hello').toBoolean() //false +S(true).toBoolean() //true +S('on').toBoolean() //true +S('yes').toBoolean() //true +S('TRUE').toBoolean() //true +S('TrUe').toBoolean() //true +S('YES').toBoolean() //true +S('ON').toBoolean() //true +S('').toBoolean() //false +S(undefined).toBoolean() //false +S('undefined').toBoolean() //false +S(null).toBoolean() //false +S(false).toBoolean() //false +S({}).toBoolean() //false +S(1).toBoolean() //true +S(-1).toBoolean() //false +S(0).toBoolean() //false + +S(['a', 'b', 'c']).toCSV().s //'"a","b","c"' +S(['a', 'b', 'c']).toCSV(':').s //'"a":"b":"c"' +S(['a', 'b', 'c']).toCSV(':', null).s //'a:b:c') +S(['a', 'b', 'c']).toCSV('*', "'").s //"'a'*'b'*'c'" +S(['a"', 'b', 4, 'c']).toCSV({delimiter: ',', qualifier: '"', escape: '\\', encloseNumbers: false}).s //'"a\\"","b",4,"c"' +S({firstName: 'JP', lastName: 'Richardson'}).toCSV({keys: true}).s //'"firstName","lastName"' +S({firstName: 'JP', lastName: 'Richardson'}).toCSV().s //'"JP","Richardson"' + +S('5').toFloat() // 5 +S('5.3').toFloat() //5.3 +S(5.3).toFloat() //5.3 +S('-10').toFloat() //-10 +S('55.3 adfafaf').toFloat() // 55.3 +S('afff 44').toFloat() //NaN +S(3.45522222333232).toFloat(2) // 3.46 + +S('5').toInt(); //5 +S('5.3').toInt(); //5; +S(5.3).toInt(); //5; +S('-10').toInt(); //-10 +S('55 adfafaf').toInt(); //55 +S('afff 44').toInt(); //NaN +S('0xff').toInt() //255 + +S('my name is JP.').capitalize().toString(); //My name is JP. +var a = "Hello " + S('joe!'); //a = "Hello joe!" +S("Hello").toString() === S("Hello").s; //true + +S('hello ').trim().s; //'hello' +S(' hello ').trim().s; //'hello' +S('\nhello').trim().s; //'hello' +S('\nhello\r\n').trim().s; //'hello' +S('\thello\t').trim().s; //'hello' + +S(' How are you?').trimLeft().s; //'How are you?'; + +S('How are you? ').trimRight().s; //'How are you?'; + +S('this is some long text').truncate(3).s //'...' +S('this is some long text').truncate(7).s //'this is...' +S('this is some long text').truncate(11).s //'this is...' +S('this is some long text').truncate(12).s //'this is some...' +S('this is some long text').truncate(11).s //'this is...' +S('this is some long text').truncate(14, ' read more').s //'this is some read more' + +S('dataRate').underscore().s; //'data_rate' +S('CarSpeed').underscore().s; //'_car_speed' +S('yesWeCan').underscore().s; //'yes_we_can' + +S('<div>hi</div>').unescapeHTML().s; //
hi
+ +S('Venkat').wrapHTML().s //Venkat +S('Venkat').wrapHTML('div').s //
Venkat
+S('Venkat').wrapHTML('div', { + "class": "left bullet" +}).s //
Venkat
+S('Venkat').wrapHTML('div', { + "id": "content", + "class": "left bullet" +}).s //
Venkat
+ +S.VERSION; //1.0.0 \ No newline at end of file diff --git a/string/string.d.ts b/string/string.d.ts new file mode 100644 index 000000000..d99a78111 --- /dev/null +++ b/string/string.d.ts @@ -0,0 +1,134 @@ +// Type definitions for string.js +// Project: http://stringjs.com +// Definitions by: Bas Pennings +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +interface StringJS { + length: number; + + s: string; + + between(left: string, right?: string): StringJS; + + camelize(): StringJS; + + capitalize(): StringJS; + + chompLeft(prefix: string): StringJS; + + chompRight(suffix: string): StringJS; + + collapseWhitespace(): StringJS; + + contains(ss: string): boolean; + + count(substring: string): number; + + dasherize(): StringJS; + + decodeHTMLEntities(): StringJS; + + endsWith(ss: string): boolean; + + escapeHTML(): StringJS; + + ensureLeft(prefix: string): StringJS; + + ensureRight(suffix: string): StringJS; + + humanize(): StringJS; + + include(ss: string): boolean; + + isAlpha(): boolean; + + isAlphaNumeric(): boolean; + + isEmpty(): boolean; + + isLower(): boolean; + + isNumeric(): boolean; + + isUpper(): boolean; + + latinise(): StringJS; + + left(n: number): StringJS; + + lines(): string[]; + + pad(len: number, char?: string|number): StringJS; + + padLeft(len: number, char?: string|number): StringJS; + + padRight(len: number, char?: string|number): StringJS; + + parseCSV(delimiter?: string, qualifier?: string, escape?: string, lineDelimiter?: string): string[]; + + repeat(n: number): StringJS; + + replaceAll(ss: string, newStr: string): StringJS; + + strip(...strings: string[]): StringJS; + + right(n: number): StringJS; + + setValue(string: any): StringJS; + + slugify(): StringJS; + + startsWith(prefix: string): boolean; + + stripPunctuation(): StringJS; + + stripTags(...tags: string[]): StringJS; + + template(values: Object, open?: string, close?: string): StringJS; + + times(n: number): StringJS; + + toBoolean(): boolean; + + toCSV(delimiter?: string, qualifier?: string): StringJS; + toCSV(options: { + delimiter?: string, + qualifier?: string, + escape?: string, + encloseNumbers?: boolean, + keys?: boolean + }): StringJS; + + toFloat(precision?: number): number; + + toInt(): number; + + toInteger(): number; + + toString(): string; + + trim(): StringJS; + + trimLeft(): StringJS; + + trimRight(): StringJS; + + truncate(length: number, chars?: string): StringJS; + + underscore(): StringJS; + + unescapeHTML(): StringJS; + + wrapHTML(element?: string, attributes?: Object): StringJS; +} + +declare module "string" { + var S: { + (o: any): StringJS; + VERSION: string; + TMPL_OPEN: string; + TMPL_CLOSE: string; + } + + export = S; +} \ No newline at end of file