From fce535beacbeefc233ab025b3b487e9768c13697 Mon Sep 17 00:00:00 2001 From: Toshimitsu Takahashi Date: Fri, 7 Feb 2025 23:11:34 +0900 Subject: [PATCH] support types for both default and named export --- .gitignore | 2 ++ .npmignore | 4 ++-- README.md | 2 +- example/example.js | 3 ++- example/example.mjs | 1 + index.d.ts | 34 ++++++++++++++++++++++++++++++++++ index.js | 1 + index.mjs | 2 +- package.json | 8 ++++++-- 9 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 index.d.ts diff --git a/.gitignore b/.gitignore index 5148e52..14e0d1b 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,8 @@ coverage # Compiled binary addons (http://nodejs.org/api/addons.html) build/Release +example/ts-cjs/dist/ +example/ts-esm/dist/ # Dependency directories node_modules diff --git a/.npmignore b/.npmignore index 5a2ecc2..34b1f18 100644 --- a/.npmignore +++ b/.npmignore @@ -1,5 +1,5 @@ coverage example test -.gitignore -.github +.* +*.tgz diff --git a/README.md b/README.md index 4ff812a..949c913 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ $ npm install -save readline-transform ```javascript const { PassThrough } = require('stream'); -const ReadlineTransform = require('readline-transform'); +const { ReadlineTransform } = require('readline-transform'); const readStream = new PassThrough(); const transform = new ReadlineTransform({ skipEmpty: true }); diff --git a/example/example.js b/example/example.js index ed59427..399aaa4 100644 --- a/example/example.js +++ b/example/example.js @@ -1,5 +1,6 @@ const { PassThrough } = require('stream'); -const ReadlineTransform = require('../'); +const { ReadlineTransform } = require('../'); +//const ReadlineTransform = require('../'); const readStream = new PassThrough(); const transform = new ReadlineTransform({ skipEmpty: true }); diff --git a/example/example.mjs b/example/example.mjs index b4c38c4..9b5a56f 100644 --- a/example/example.mjs +++ b/example/example.mjs @@ -1,6 +1,7 @@ import fs from 'node:fs'; import { PassThrough } from 'node:stream'; import { ReadlineTransform } from '../index.mjs'; +//import ReadlineTransform from '../index.mjs'; const transform = new ReadlineTransform({ skipEmpty: true }); const fileStream = fs.createReadStream('./data.txt'); diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..bb1b857 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,34 @@ +/// + +import { Transform, TransformOptions } from "stream"; + +declare namespace ReadlineTransform { + interface ReadlineTransformOptions extends TransformOptions { + /** + * line break matcher for str.split() + * @default /\r?\n/ + */ + breakMatcher?: RegExp | undefined; + + /** + * if content ends with line break, ignore last empty line + * @default true + */ + + ignoreEndOfBreak?: boolean | undefined; + /** + * if line is empty string, skip it + * @default false + */ + skipEmpty?: boolean | undefined; + } +} + +declare class ReadlineTransform extends Transform { + constructor(options?: ReadlineTransform.ReadlineTransformOptions); + + static default: typeof ReadlineTransform; + static ReadlineTransform: typeof ReadlineTransform; +} + +export = ReadlineTransform; diff --git a/index.js b/index.js index 9616a4e..1227b40 100644 --- a/index.js +++ b/index.js @@ -74,3 +74,4 @@ class ReadlineTransform extends Transform { } module.exports = exports.default = ReadlineTransform; +module.exports.ReadlineTransform = ReadlineTransform; diff --git a/index.mjs b/index.mjs index 2330860..e59e58b 100644 --- a/index.mjs +++ b/index.mjs @@ -1 +1 @@ -export { default as ReadlineTransform } from './index.js'; +export { ReadlineTransform, default } from './index.js'; diff --git a/package.json b/package.json index 8eaad9a..660af4c 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,15 @@ { "name": "readline-transform", - "version": "1.0.0", + "version": "2.0.0", "description": "Transform stream to read line-by-line and write a string", "main": "index.js", + "types": "index.d.ts", "exports": { ".": { - "import": "./index.mjs", + "import": { + "default": "./index.mjs", + "types": "./index.d.ts" + }, "require": "./index.js" } },