Snippets for Typescript
Dates, Files, and string functions
Checksum
The shasum of this package was 0d9124d61ace018726d98de4fafbbde9d570e309
Install
npm install --save klingsten-snippets --latest
Dependencies
import
import { Numbers, Files } from "klingsten-snippets";
const { Numbers, Log, Dates } = require('klingsten-snippets');
Usage / API
Number
import { Numbers } from "klingsten-snippets";
Numbers.roundWithPrecision ( number, integer);
Numbers.isNumeric(input: any)
Numbers.round(n: number): number
Numbers.getCounter()
Files
import { Files, IUploadFile } from "klingsten-snippets";
public static async uploadFiles(filesInfo: File[]): Promise<IUploadFile[]>
public static async readFileAsync(file: File): Promise<string> {
Usage
HTML:
<input #file type="file" multiple (change)="upload(file.files)">
HTML -> example from Angular[v2+]
<div *ngFor="let file of files">
<img src="{{file.data}}" height="100px" max-width="200">
</div>
files: IUploadFile[] = [];
async upload(files:File[]) {
this.files = await Files.uploadfiles(files)
}
Strings
import { Strings } from "klingsten-snippets";
Strings.splitExceptQuotes(s: string, delimitor1: string = ';', delimitor2: string = ','): string[]
public static removeHtmlTags(html: string): string
Strings.stringToNumber("129.12", true);
Strings.splitNameFromEmail(s: string): { name: string, email: string }
Strings.uniqueID(): string
Strings.replaceIllgalCharsEmail(s: string): string
Strings.isEmailValid(s: string): boolean
Examples:
const emailStr = ' "lars, K" <l@email.dk>; lars@email.com, Bob, Lin <sn@g.com> ';
const nameEmailArr : = Strings.splitNameFromEmail( emailStr );
[ { name: "lars, K", email: "l@email.dk" },
{ name: "", email: "lars@email.com" },
{ name: "Bob, Lin",email: "sn@g.com" }
]
const email0 = Strings.splitNameFromEmail(strArr[0]);
const email1 = Strings.splitNameFromEmail(strArr[1]);
Dates
import { Dates } from "klingsten-snippets";
Dates.epochToIsoString (number) : string
Dates.epochToISODateTime (number) : string
Dates.parseStrToDateUtc(dateStr: string, dateFormat: string) : Date | undefined
Dates.parseStrToDateTimeUtc(dateStr: string, dateFormat: string) : Date | undefined
Dates.removeHours(d: Date) : Date | undefined
Compare
import { Compare } from "klingsten-snippets";
Compare.arrays(arrA: any[], arrB: any[], attr: string[]): CompareError[]
Compare.objects(objA: any[], objB: any[], attr: string[]): CompareError[]
Compare.printErrors(errors: CompareError[], funcName: string))
Example:
test_splitNameFromEmail() {
const test = {
name: "Strings.splitNameFromEmail()",
insAndOuts: [
{ in: 'Lars', exp: { name: 'Lars', email: '', isValid: false } },
{ in: 'lars@EMAIL.com', exp: { name: '', email: 'lars@email.com', isValid: true }
]
}
let successCount = 0;
for (let i = 0; i < test.insAndOuts.length; i++) {
const r = test.insAndOuts[i];
const result = Strings.splitNameFromEmail(r.in)
const errors = Compare.objects(result, r.exp, ['name', 'email']);
const isSuccess = errors.length === 0;
if (isSuccess) { successCount++; }
else { Compare.printErrors(errors, test.name + ' i=' + i) }
}
console.log("isSuccess", test.insAndOuts.length === successCount, `${test.name} tests=${test.insAndOuts.length} success=${successCount} `);
}
Timer
import { Timer } from "klingsten-snippets";
Timer.start('test');
console.log(Timer.stop('test'));
console.log(Timer.stopAsString('test'));
Timer.stopAsLog('test');
Log
import { Log } from "klingsten-snippets";
Log.log("message")
Log.now()
Log.formatDateToUTCString(date)
Interfaces
interface IUploadFile {
info: File;
data: string;
}
source code
https://bitbucket.org/LarsKlingsten/klingsten-snippets-ts