typescript-string-operations
Advanced tools
Comparing version
@@ -58,14 +58,7 @@ "use strict"; | ||
try { | ||
return format.replace(/{(\d+(:\w*)?)}/g, function (match, i) { | ||
var s = match.split(':'); | ||
if (s.length > 1) { | ||
i = i[0]; | ||
match = s[1].replace('}', ''); | ||
} | ||
var arg = args[i]; | ||
if (arg == null || arg == undefined || match.match(/{d+}/)) | ||
return arg; | ||
arg = String.parsePattern(match, arg); | ||
return typeof arg != 'undefined' && arg != null ? arg : String.Empty; | ||
}); | ||
if (format.match(String.regexNumber)) | ||
return String.format(String.regexNumber, format, args); | ||
if (format.match(String.regexObject)) | ||
return String.format(String.regexObject, format, args, true); | ||
return String.Empty; | ||
} | ||
@@ -77,2 +70,21 @@ catch (e) { | ||
}; | ||
String.format = function (regex, format, args, parseByObject) { | ||
if (parseByObject === void 0) { parseByObject = false; } | ||
return format.replace(regex, function (match, x) { | ||
var s = match.split(':'); | ||
if (s.length > 1) { | ||
x = s[0].replace('{', ''); | ||
match = s[1].replace('}', ''); | ||
} | ||
var arg; | ||
if (parseByObject) | ||
arg = args[0][x]; | ||
else | ||
arg = args[x]; | ||
if (arg == null || arg == undefined || match.match(/{\d+}/)) | ||
return arg; | ||
arg = String.parsePattern(match, arg); | ||
return typeof arg != 'undefined' && arg != null ? arg : String.Empty; | ||
}); | ||
}; | ||
String.parsePattern = function (match, arg) { | ||
@@ -103,2 +115,4 @@ switch (match) { | ||
case 'n': | ||
if (typeof (arg) !== "string") | ||
arg = arg.toString(); | ||
var replacedString = arg.replace(/,/g, '.'); | ||
@@ -187,3 +201,5 @@ if (isNaN(parseFloat(replacedString)) || replacedString.length <= 3) | ||
}; | ||
String.Empty = ""; | ||
String.regexNumber = /{(\d+(:\w*)?)}/g; | ||
String.regexObject = /{(\w+(:\w*)?)}/g; | ||
String.Empty = ''; | ||
return String; | ||
@@ -190,0 +206,0 @@ }()); |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var String=function(){function t(){}return t.IsNullOrWhiteSpace=function(t){try{return null==t||"undefined"==t||t.toString().replace(/\s/g,"").length<1}catch(t){return console.log(t),!1}},t.Join=function(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];try{var a=e[0];if(Array.isArray(a)||a instanceof Array){for(var i=t.Empty,l=0;l<a.length;l++){var o=a[l];l<a.length-1?i+=o+r:i+=o}return i}if("object"==typeof a){var u=t.Empty,g=a;return Object.keys(a).forEach(function(t){u+=g[t]+r}),u=u.slice(0,u.length-r.length)}var s=e;return t.join.apply(t,[r].concat(s))}catch(r){return console.log(r),t.Empty}},t.Format=function(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];try{return r.replace(/{(\d+(:\w*)?)}/g,function(r,n){var a=r.split(":");a.length>1&&(n=n[0],r=a[1].replace("}",""));var i=e[n];return null==i||void 0==i||r.match(/{d+}/)?i:void 0!==(i=t.parsePattern(r,i))&&null!=i?i:t.Empty})}catch(r){return console.log(r),t.Empty}},t.parsePattern=function(r,e){switch(r){case"L":return e=e.toLowerCase();case"U":return e=e.toUpperCase();case"d":if("string"==typeof e)return t.getDisplayDateFromString(e);if(e instanceof Date)return t.Format("{0:00}.{1:00}.{2:0000}",e.getDate(),e.getMonth(),e.getFullYear());break;case"s":if("string"==typeof e)return t.getSortableDateFromString(e);if(e instanceof Date)return t.Format("{0:0000}-{1:00}-{2:00}",e.getFullYear(),e.getMonth(),e.getDate());break;case"n":var n=e.replace(/,/g,".");if(isNaN(parseFloat(n))||n.length<=3)break;var a=n.split(/[^0-9]+/g),i=a;a.length>1&&(i=[t.join.apply(t,[""].concat(a.splice(0,a.length-1))),a[a.length-1]]);var l=i[0],o=l.length%3,u=o>0?l.substring(0,o):t.Empty,g=l.substring(o).match(/.{3}/g);return e=(u=u+"."+t.Join(".",g))+(i.length>1?","+i[1]:"")}return"number"!=typeof e&&isNaN(e)||isNaN(+r)||t.IsNullOrWhiteSpace(e)?e:t.formatNumber(e,r)},t.getDisplayDateFromString=function(t){var r;if((r=t.split("-")).length<=1)return t;var e=r[r.length-1],n=r[r.length-2],a=r[r.length-3];return(e=(e=e.split("T")[0]).split(" ")[0])+"."+n+"."+a},t.getSortableDateFromString=function(r){var e=r.replace(",","").split(".");if(e.length<=1)return r;var n=e[e.length-1].split(" "),a=t.Empty;n.length>1&&(a=n[n.length-1]);var i=e[e.length-1].split(" ")[0]+"-"+e[e.length-2]+"-"+e[e.length-3];return!t.IsNullOrWhiteSpace(a)&&a.length>1?i+="T"+a:i+="T00:00:00",i},t.formatNumber=function(t,r){var e=r.length,n=t.toString();if(e<=n.length)return n;var a=e-n.length;return new Array(a+=1).join("0")+n},t.join=function(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];for(var a=t.Empty,i=0;i<e.length;i++)if(!("string"==typeof e[i]&&t.IsNullOrWhiteSpace(e[i])||"number"!=typeof e[i]&&"string"!=typeof e[i])){a+=""+e[i];for(var l=i+1;l<e.length;l++)if(!t.IsNullOrWhiteSpace(e[l])){a+=r,i=l-1;break}}return a},t.Empty="",t}();exports.String=String;var StringBuilder=function(){function t(t){void 0===t&&(t=String.Empty),this.Values=[],this.Values=new Array(t)}return t.prototype.ToString=function(){return this.Values.join("")},t.prototype.Append=function(t){this.Values.push(t)},t.prototype.AppendFormat=function(t){for(var r=[],e=1;e<arguments.length;e++)r[e-1]=arguments[e];this.Values.push(String.Format.apply(String,[t].concat(r)))},t.prototype.Clear=function(){this.Values=[]},t}();exports.StringBuilder=StringBuilder; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var String=function(){function t(){}return t.IsNullOrWhiteSpace=function(t){try{return null==t||"undefined"==t||t.toString().replace(/\s/g,"").length<1}catch(t){return console.log(t),!1}},t.Join=function(e){for(var r=[],n=1;n<arguments.length;n++)r[n-1]=arguments[n];try{var a=r[0];if(Array.isArray(a)||a instanceof Array){for(var i=t.Empty,o=0;o<a.length;o++){var l=a[o];o<a.length-1?i+=l+e:i+=l}return i}if("object"==typeof a){var g=t.Empty,u=a;return Object.keys(a).forEach(function(t){g+=u[t]+e}),g=g.slice(0,g.length-e.length)}var p=r;return t.join.apply(t,[e].concat(p))}catch(e){return console.log(e),t.Empty}},t.Format=function(e){for(var r=[],n=1;n<arguments.length;n++)r[n-1]=arguments[n];try{return e.match(t.regexNumber)?t.format(t.regexNumber,e,r):e.match(t.regexObject)?t.format(t.regexObject,e,r,!0):t.Empty}catch(e){return console.log(e),t.Empty}},t.format=function(e,r,n,a){return void 0===a&&(a=!1),r.replace(e,function(e,r){var i,o=e.split(":");return o.length>1&&(r=o[0].replace("{",""),e=o[1].replace("}","")),null==(i=a?n[0][r]:n[r])||void 0==i||e.match(/{\d+}/)?i:void 0!==(i=t.parsePattern(e,i))&&null!=i?i:t.Empty})},t.parsePattern=function(e,r){switch(e){case"L":return r=r.toLowerCase();case"U":return r=r.toUpperCase();case"d":if("string"==typeof r)return t.getDisplayDateFromString(r);if(r instanceof Date)return t.Format("{0:00}.{1:00}.{2:0000}",r.getDate(),r.getMonth(),r.getFullYear());break;case"s":if("string"==typeof r)return t.getSortableDateFromString(r);if(r instanceof Date)return t.Format("{0:0000}-{1:00}-{2:00}",r.getFullYear(),r.getMonth(),r.getDate());break;case"n":"string"!=typeof r&&(r=r.toString());var n=r.replace(/,/g,".");if(isNaN(parseFloat(n))||n.length<=3)break;var a=n.split(/[^0-9]+/g),i=a;a.length>1&&(i=[t.join.apply(t,[""].concat(a.splice(0,a.length-1))),a[a.length-1]]);var o=i[0],l=o.length%3,g=l>0?o.substring(0,l):t.Empty,u=o.substring(l).match(/.{3}/g);return r=(g=g+"."+t.Join(".",u))+(i.length>1?","+i[1]:"")}return"number"!=typeof r&&isNaN(r)||isNaN(+e)||t.IsNullOrWhiteSpace(r)?r:t.formatNumber(r,e)},t.getDisplayDateFromString=function(t){var e;if((e=t.split("-")).length<=1)return t;var r=e[e.length-1],n=e[e.length-2],a=e[e.length-3];return(r=(r=r.split("T")[0]).split(" ")[0])+"."+n+"."+a},t.getSortableDateFromString=function(e){var r=e.replace(",","").split(".");if(r.length<=1)return e;var n=r[r.length-1].split(" "),a=t.Empty;n.length>1&&(a=n[n.length-1]);var i=r[r.length-1].split(" ")[0]+"-"+r[r.length-2]+"-"+r[r.length-3];return!t.IsNullOrWhiteSpace(a)&&a.length>1?i+="T"+a:i+="T00:00:00",i},t.formatNumber=function(t,e){var r=e.length,n=t.toString();if(r<=n.length)return n;var a=r-n.length;return new Array(a+=1).join("0")+n},t.join=function(e){for(var r=[],n=1;n<arguments.length;n++)r[n-1]=arguments[n];for(var a=t.Empty,i=0;i<r.length;i++)if(!("string"==typeof r[i]&&t.IsNullOrWhiteSpace(r[i])||"number"!=typeof r[i]&&"string"!=typeof r[i])){a+=""+r[i];for(var o=i+1;o<r.length;o++)if(!t.IsNullOrWhiteSpace(r[o])){a+=e,i=o-1;break}}return a},t.regexNumber=/{(\d+(:\w*)?)}/g,t.regexObject=/{(\w+(:\w*)?)}/g,t.Empty="",t}();exports.String=String;var StringBuilder=function(){function t(t){void 0===t&&(t=String.Empty),this.Values=[],this.Values=new Array(t)}return t.prototype.ToString=function(){return this.Values.join("")},t.prototype.Append=function(t){this.Values.push(t)},t.prototype.AppendFormat=function(t){for(var e=[],r=1;r<arguments.length;r++)e[r-1]=arguments[r];this.Values.push(String.Format.apply(String,[t].concat(e)))},t.prototype.Clear=function(){this.Values=[]},t}();exports.StringBuilder=StringBuilder; |
export class String { | ||
public static Empty: string = ""; | ||
private static readonly regexNumber = /{(\d+(:\w*)?)}/g; | ||
private static readonly regexObject = /{(\w+(:\w*)?)}/g; | ||
public static Empty: string = ''; | ||
public static IsNullOrWhiteSpace(value: string): boolean { | ||
@@ -54,16 +57,9 @@ try { | ||
try { | ||
return format.replace(/{(\d+(:\w*)?)}/g, function (match, i) { //0 | ||
let s = match.split(':'); | ||
if (s.length > 1) { | ||
i = i[0]; | ||
match = s[1].replace('}', ''); //U | ||
} | ||
if (format.match(String.regexNumber)) | ||
return String.format(String.regexNumber, format, args); | ||
let arg = args[i]; | ||
if (arg == null || arg == undefined || match.match(/{d+}/)) | ||
return arg; | ||
arg = String.parsePattern(match, arg); | ||
return typeof arg != 'undefined' && arg != null ? arg : String.Empty; | ||
}); | ||
if (format.match(String.regexObject)) | ||
return String.format(String.regexObject, format, args, true); | ||
return String.Empty; | ||
} | ||
@@ -76,2 +72,24 @@ catch (e) { | ||
private static format(regex: any, format: string, args: any, parseByObject: boolean = false): string { | ||
return format.replace(regex, function (match, x) { //0 | ||
let s = match.split(':'); | ||
if (s.length > 1) { | ||
x = s[0].replace('{', ''); | ||
match = s[1].replace('}', ''); //U | ||
} | ||
let arg; | ||
if (parseByObject) | ||
arg = args[0][x]; | ||
else | ||
arg = args[x]; | ||
if (arg == null || arg == undefined || match.match(/{\d+}/)) | ||
return arg; | ||
arg = String.parsePattern(match, arg); | ||
return typeof arg != 'undefined' && arg != null ? arg : String.Empty; | ||
}); | ||
} | ||
private static parsePattern(match: 'L' | 'U' | 'd' | 's' | 'n' | string, arg: string | Date | number | any): string { | ||
@@ -102,3 +120,5 @@ switch (match) { | ||
case 'n': //Tausender Trennzeichen | ||
let replacedString = arg.replace(/,/g,'.'); | ||
if (typeof (arg) !== "string") | ||
arg = arg.toString(); | ||
let replacedString = arg.replace(/,/g, '.'); | ||
if (isNaN(parseFloat(replacedString)) || replacedString.length <= 3) | ||
@@ -110,4 +130,4 @@ break; | ||
if(numberparts.length > 1){ | ||
parts = [String.join('',...(numberparts.splice(0, numberparts.length -1 ))), numberparts[numberparts.length-1]]; | ||
if (numberparts.length > 1) { | ||
parts = [String.join('', ...(numberparts.splice(0, numberparts.length - 1))), numberparts[numberparts.length - 1]]; | ||
} | ||
@@ -121,4 +141,4 @@ | ||
var remainingGroups = integer.substring(mod).match(/.{3}/g); | ||
output = output + '.' + String.Join('.',remainingGroups); | ||
arg = output + (parts.length > 1 ? ','+ parts[1] : ''); | ||
output = output + '.' + String.Join('.', remainingGroups); | ||
arg = output + (parts.length > 1 ? ',' + parts[1] : ''); | ||
return arg; | ||
@@ -159,3 +179,3 @@ default: | ||
if (times.length > 1) | ||
time = times[times.length - 1]; | ||
time = times[times.length - 1]; | ||
@@ -166,7 +186,7 @@ let year = splitted[splitted.length - 1].split(' ')[0]; | ||
let result = `${year}-${month}-${day}` | ||
if (!String.IsNullOrWhiteSpace(time) && time.length > 1) | ||
result += `T${time}`; | ||
else | ||
result += "T00:00:00"; | ||
result += "T00:00:00"; | ||
@@ -178,3 +198,3 @@ return result; | ||
let count = formatTemplate.length; | ||
let stringValue = input.toString(); | ||
let stringValue = input.toString(); | ||
if (count <= stringValue.length) | ||
@@ -181,0 +201,0 @@ return stringValue; |
{ | ||
"name": "typescript-string-operations", | ||
"version": "1.2.5", | ||
"version": "1.3.0", | ||
"description": "Simple lightweight string operation library for Typescript, works with Angular", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.min.js", |
@@ -48,2 +48,18 @@ [](https://www.npmjs.com/package/typescript-string-operations) | ||
## UPDATE | ||
#### String Format for Objects including specifiers | ||
```typescript | ||
var fruit = new Fruit(); | ||
fruit.type = "apple"; | ||
fruit.color = "RED"; | ||
fruit.shippingDate = ""; | ||
fruit.amount = 10000; | ||
String.Format("the {type:U} is {color:L} shipped on {shippingDate:s} with an amount of {amount:n}", fruit); | ||
// output: the APPLE is red shipped on with an amount of 10.000 | ||
``` | ||
| Specifier | Result | | ||
@@ -50,0 +66,0 @@ | :-------------: |:---------------------------:| |
import { String } from '../dist/index'; | ||
import { Fruit } from './fruit'; | ||
import { expect } from 'chai'; | ||
@@ -32,3 +33,3 @@ import 'mocha'; | ||
describe('String.Format', () => { | ||
describe('String.Format Number Pattern', () => { | ||
describe('Placeholders', () => { | ||
@@ -183,2 +184,28 @@ it('should format the string correct', () => { | ||
describe('String.Format Text Pattern', () => { | ||
describe("formatting", () => { | ||
it("Should parse out the word", () => { | ||
// Arrange | ||
var fruit = { type: "apple", color: "red" }; | ||
// Act | ||
var formatted = String.Format("the {type} is {color}", fruit); | ||
// Assert | ||
expect(formatted).to.equal("the apple is red"); | ||
}); | ||
it("Should parse out the word with specifiers and TS Class", () => { | ||
// Arrange | ||
var fruit: Fruit = new Fruit("apple", "RED", "31.12.2018 01:02:03", "10000"); | ||
// Act | ||
var formatted = String.Format("the {type:U} is {color:L} shipped on {shippingDate:s} with an amount of {amount:n}", fruit); | ||
// Assert | ||
expect(formatted).to.equal("the APPLE is red shipped on 2018-12-31T01:02:03 with an amount of 10.000"); | ||
}); | ||
}); | ||
}); | ||
describe('String.Join', () => { | ||
@@ -185,0 +212,0 @@ it('should join the given strings passed as args', () => { |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
36477
9.87%12
9.09%640
10.92%133
13.68%0
-100%