Skip to content

Commit 854f106

Browse files
committed
refactor: convert fns package to TS
1 parent 2d5afe0 commit 854f106

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+376
-276
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474
"rimraf": "^2.6.3",
7575
"rollup": "^4.34.6",
7676
"rollup-plugin-copy": "^3.5.0",
77+
"rollup-plugin-delete": "^2.1.0",
78+
"rollup-plugin-dts": "^6.1.1",
7779
"rollup-plugin-ignore": "^1.0.10",
7880
"rollup-plugin-local-resolve": "^1.0.7",
7981
"rollup-plugin-polyfill-node": "^0.13.0",

packages/fns/babel.config.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/fns/package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@
77
"homepage": "https://github.com/diegomura/react-pdf#readme",
88
"type": "module",
99
"main": "./lib/index.js",
10+
"types": "./lib/index.d.ts",
1011
"repository": {
1112
"type": "git",
1213
"url": "https://github.com/diegomura/react-pdf.git",
1314
"directory": "packages/fns"
1415
},
1516
"scripts": {
1617
"test": "vitest",
17-
"build": "rimraf ./lib && rollup -c",
18-
"watch": "rimraf ./lib && rollup -c -w"
19-
},
20-
"dependencies": {
21-
"@babel/runtime": "^7.20.13"
18+
"build": "rollup -c",
19+
"watch": "rollup -c -w"
2220
},
2321
"files": [
2422
"lib"

packages/fns/rollup.config.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
1-
import babel from '@rollup/plugin-babel';
1+
import typescript from '@rollup/plugin-typescript';
2+
import { dts } from 'rollup-plugin-dts';
3+
import del from 'rollup-plugin-delete';
24

3-
const input = 'src/index.js';
4-
5-
const output = { format: 'es', file: 'lib/index.js' };
6-
7-
const getExternal = () => [/@babel\/runtime/];
8-
9-
const getPlugins = () => [
10-
babel({
11-
babelrc: true,
12-
babelHelpers: 'runtime',
13-
exclude: 'node_modules/**',
14-
}),
5+
const config = [
6+
{
7+
input: 'src/index.ts',
8+
output: { format: 'es', dir: 'lib' },
9+
plugins: [typescript(), del({ targets: 'lib' })],
10+
},
11+
{
12+
input: './lib/types/index.d.ts',
13+
output: [{ file: 'lib/index.d.ts', format: 'es' }],
14+
plugins: [dts(), del({ targets: 'lib/types', hook: 'buildEnd' })],
15+
},
1516
];
1617

17-
const config = {
18-
input,
19-
output,
20-
external: getExternal(),
21-
plugins: getPlugins(),
22-
};
23-
2418
export default config;
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* Applies a function to the value at the given index of an array
3-
4-
* @param {number} index
5-
* @param {function} fn
6-
* @param {array} collection
7-
* @returns copy of the array with the element at the given index replaced with the result of the function application.
3+
*
4+
* @param index
5+
* @param fn
6+
* @param collection
7+
* @returns Copy of the array with the element at the given index replaced with the result of the function application.
88
*/
9-
const adjust = (index, fn, collection) => {
9+
const adjust = (index: number, fn: (value: any) => any, collection: any[]) => {
1010
if (index >= 0 && index >= collection.length) return collection;
1111
if (index < 0 && Math.abs(index) > collection.length) return collection;
1212

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,15 @@
22

33
import reverse from './reverse';
44

5-
/**
6-
* @typedef {Function} AsyncCompose
7-
* @param {any} value
8-
* @param {...any} args
9-
* @returns {any} result
10-
*/
11-
125
/**
136
* Performs right-to-left function composition with async functions support
147
*
15-
* @param {...Function} fns functions
16-
* @returns {AsyncCompose} composed function
8+
* @param fns - Functions
9+
* @returns Composed function
1710
*/
1811
const asyncCompose =
19-
(...fns) =>
20-
async (value, ...args) => {
12+
(...fns: any[]) =>
13+
async (value: any, ...args: any[]) => {
2114
let result = value;
2215
const reversedFns = reverse(fns);
2316

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/**
22
* Capitalize first letter of each word
33
*
4-
* @param {string} value string
5-
* @returns {string} capitalized string
4+
* @param value = Any string
5+
* @returns Capitalized string
66
*/
7-
const capitalize = (value) => {
7+
const capitalize = (value?: string | null) => {
88
if (!value) return value;
99
return value.replace(/(^|\s)\S/g, (l) => l.toUpperCase());
1010
};

packages/fns/src/castArray.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

packages/fns/src/castArray.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Casts value to array
3+
*
4+
* @template T - The type of the value.
5+
* @param value - The value to cast into an array.
6+
* @returns An array containing the given value.
7+
*/
8+
const castArray = <T = any>(value: T | T[]): T[] => {
9+
return Array.isArray(value) ? value : [value];
10+
};
11+
12+
export default castArray;
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
import reverse from './reverse';
22

3-
/**
4-
* @typedef {Function} Compose
5-
* @param {any} value
6-
* @param {...any} args
7-
* @returns {any} result
8-
*/
9-
103
/**
114
* Performs right-to-left function composition
125
*
13-
* @param {...Function} fns functions
14-
* @returns {Compose} composed function
6+
* @param fns - Functions
7+
* @returns Composed function
158
*/
169
const compose =
17-
(...fns) =>
18-
(value, ...args) => {
10+
(...fns: any[]) =>
11+
(value: any, ...args: any[]) => {
1912
let result = value;
2013
const reversedFns = reverse(fns);
2114

0 commit comments

Comments
 (0)