-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix incorrect type-declarations * Add a proper return-type * Refactor the documentation-comments * Downgrade the `package-lock.json` to v1 * Add missing export-statement * Remove redundant type-declaration * Make argument-name more apropriate * Add a test for checking whether `gulp-replace` works with typescript * Create separate type-declarations for `Replacer` and `Options` * Make `skipBinary` optional * Create type-declarations for the `file`-property * Add documentation-comments * Add tests for the new type-declarations * Increase timeout of slow tests * Add `@types/vinyl` to the dependencies
- Loading branch information
Showing
5 changed files
with
203 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,53 @@ | ||
/** | ||
* replace | ||
* gulp-replace can be called with a string or regex. | ||
* | ||
* @param search The string or regex to search for | ||
* | ||
* @param _replacement The replacement string or function. | ||
* <p>If replacement is a function, it will be called once for each match and will be passed the string | ||
* that is to be replaced. The value of `this.file` will be equal to the vinyl instance for the file | ||
* being processed.</p> | ||
* Read more at | ||
* <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter">String.prototype.replace() at MDN web docs</a> | ||
* | ||
* @param options `options.skipBinary` will be equal to `true` by default. | ||
* <p>Skip binary files. This option is true by default. If | ||
* you want to replace content in binary files, you must explicitly set it to false</p> | ||
* | ||
*/ | ||
export declare function replace( | ||
search: string | RegExp, | ||
_replacement: string | (() => string) | ((search: string, ...args: any[]) => string), | ||
options?: { skipBinary: boolean } | ||
): any; /* The type of return value should not be `any`, but I could not find the types definition of */ | ||
/// <reference types="node" /> | ||
import type File = require("vinyl"); | ||
|
||
/** | ||
* Represents options for `gulp-replace`. | ||
*/ | ||
interface Options { | ||
/** | ||
* A value indicating whether binary files should be skipped. | ||
*/ | ||
skipBinary?: boolean | ||
} | ||
|
||
/** | ||
* The context of the replacer-function. | ||
*/ | ||
interface ReplacerContext { | ||
/** | ||
* The file being processed. | ||
*/ | ||
file: File | ||
} | ||
|
||
/** | ||
* Represents a method for replacing contents of a vinyl-file. | ||
*/ | ||
type Replacer = (this: ReplacerContext, match: string, ...args: any[]) => string; | ||
|
||
/** | ||
* Searches and replaces a portion of text using a `string` or a `RegExp`. | ||
* | ||
* @param search The `string` or `RegExp` to search for. | ||
* | ||
* @param replacement The replacement string or a function for generating a replacement. | ||
* | ||
* If `replacement` is a function, it will be called once for each match and will be passed the string | ||
* that is to be replaced. The value of `this.file` will be equal to the vinyl instance for the file | ||
* being processed. | ||
* | ||
* Read more at [`String.prototype.replace()` at MDN web docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter"). | ||
* | ||
* @param options `options.skipBinary` will be equal to `true` by default. | ||
* | ||
* Skip binary files. This option is `true` by default. If | ||
* you want to replace content in binary files, you must explicitly set it to `false`. | ||
*/ | ||
declare function replace( | ||
search: string | RegExp, | ||
replacement: string | Replacer, | ||
options?: Options | ||
): NodeJS.ReadWriteStream; | ||
|
||
export = replace; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import replacePlugin = require('../..'); | ||
|
||
replacePlugin(/.*/, ''); | ||
replacePlugin('hello', 'world'); | ||
|
||
replacePlugin(/.*/, () => 'test'); | ||
replacePlugin('', (match, ...args) => match + '-test' + args[0]); | ||
|
||
replacePlugin( | ||
'', | ||
function (match) | ||
{ | ||
console.log(match); | ||
console.log(this.file.basename); | ||
return ''; | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters