From abb6102c14cecadff82669f450facd9a4abe713c Mon Sep 17 00:00:00 2001 From: Piotr Roszatycki Date: Thu, 11 Oct 2018 19:44:22 +0200 Subject: [PATCH] replacestream: new typings (#29608) * replacestream: new typings * replacestream: fix "Exceeds maximum line length of 200" * replacestream: use `export =` syntax --- types/replacestream/index.d.ts | 54 ++++++++++++++++++++++ types/replacestream/replacestream-tests.ts | 27 +++++++++++ types/replacestream/tsconfig.json | 23 +++++++++ types/replacestream/tslint.json | 1 + 4 files changed, 105 insertions(+) create mode 100644 types/replacestream/index.d.ts create mode 100644 types/replacestream/replacestream-tests.ts create mode 100644 types/replacestream/tsconfig.json create mode 100644 types/replacestream/tslint.json diff --git a/types/replacestream/index.d.ts b/types/replacestream/index.d.ts new file mode 100644 index 0000000000..ede4591a1d --- /dev/null +++ b/types/replacestream/index.d.ts @@ -0,0 +1,54 @@ +// Type definitions for replacestream 4.0 +// Project: https://github.com/eugeneware/replacestream#readme +// Definitions by: Piotr Roszatycki +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare namespace ReplaceStream { + interface Options { + /** + * Sets a limit on the number of times the replacement will be made. This + * is forced to one when a regex without the global flag is provided. + * + * Default: `Infinity` + */ + limit?: number; + /** + * The text encoding used during search and replace. + * + * Default: `"utf8"` + */ + encoding?: string; + /** + * When doing cross-chunk replacing, this sets the maximum length match + * that will be supported. + * + * Default: `100` + */ + maxMatchLen?: number; + /** + * When doing string match (not relevant for regex matching) whether to do a + * case insensitive search. + * + * Default: `true` + */ + ignoreCase?: boolean; + /** + * When provided, these flags will be used when creating the search regexes + * internally. + * + * @deprecated as the flags set on the regex provided are no longer mutated + * if this is not provided. + */ + regExpOptions?: string; + } + + type ReplaceFunction = (match: string, p1: string, offset: number, string: string) => string; +} + +declare function ReplaceStream( + search: RegExp | string, + replace: ReplaceStream.ReplaceFunction | string, + options?: ReplaceStream.Options +): any; + +export = ReplaceStream; diff --git a/types/replacestream/replacestream-tests.ts b/types/replacestream/replacestream-tests.ts new file mode 100644 index 0000000000..d3062af185 --- /dev/null +++ b/types/replacestream/replacestream-tests.ts @@ -0,0 +1,27 @@ +/// + +import * as fs from 'fs'; +import * as path from 'path'; +import replaceStream = require('replacestream'); + +fs.createReadStream(path.join(__dirname, 'happybirthday.txt')) + .pipe(replaceStream('birthday', 'earthday')) + .pipe(process.stdout); + +fs.createReadStream(path.join(__dirname, 'happybirthday.txt')) + .pipe(replaceStream('birthday', 'earthday', { limit: 2 })) + .pipe(process.stdout); + +fs.createReadStream(path.join(__dirname, 'happybirthday.txt')) + .pipe(replaceStream(/birthday/, 'earthday')) + .pipe(process.stdout); + +const words = ['Awesome', 'Good', 'Super', 'Joyous']; + +function replaceFn(match: string, p1: string, offset: number, string: string): string { + return words.shift() || 'Happy'; +} + +fs.createReadStream(path.join(__dirname, 'happybirthday.txt')) + .pipe(replaceStream('Happy', replaceFn)) + .pipe(process.stdout); diff --git a/types/replacestream/tsconfig.json b/types/replacestream/tsconfig.json new file mode 100644 index 0000000000..96bfd055fa --- /dev/null +++ b/types/replacestream/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "replacestream-tests.ts" + ] +} diff --git a/types/replacestream/tslint.json b/types/replacestream/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/replacestream/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }