replacestream: new typings (#29608)

* replacestream: new typings

* replacestream: fix "Exceeds maximum line length of 200"

* replacestream: use `export =` syntax
This commit is contained in:
Piotr Roszatycki
2018-10-11 19:44:22 +02:00
committed by Andy
parent 7fd8a1b5a5
commit abb6102c14
4 changed files with 105 additions and 0 deletions

54
types/replacestream/index.d.ts vendored Normal file
View File

@@ -0,0 +1,54 @@
// Type definitions for replacestream 4.0
// Project: https://github.com/eugeneware/replacestream#readme
// Definitions by: Piotr Roszatycki <https://github.com/dex4er>
// 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;

View File

@@ -0,0 +1,27 @@
/// <reference types="node"/>
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);

View File

@@ -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"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }