From 4102b8a3fd4a71016f30ee9791a3ffbbd3595274 Mon Sep 17 00:00:00 2001 From: Tsung-Ju Chiang <71379180+tsung-ju@users.noreply.github.com> Date: Wed, 23 Jun 2021 03:04:44 +0800 Subject: [PATCH] Remove dependency on react-children-utilities --- package-lock.json | 76 ----------------------------------------------- package.json | 2 -- rollup.config.js | 3 +- src/index.tsx | 15 ++++++++-- 4 files changed, 14 insertions(+), 82 deletions(-) diff --git a/package-lock.json b/package-lock.json index f1b190c..1864e65 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,43 +4,6 @@ "lockfileVersion": 1, "requires": true, "dependencies": { - "@rollup/plugin-node-resolve": { - "version": "13.0.0", - "resolved": "https://registry.npmjs.org/@rollup/plugin-node-resolve/-/plugin-node-resolve-13.0.0.tgz", - "integrity": "sha512-41X411HJ3oikIDivT5OKe9EZ6ud6DXudtfNrGbC4nniaxx2esiWjkLOzgnZsWq1IM8YIeL2rzRGLZLBjlhnZtQ==", - "dev": true, - "requires": { - "@rollup/pluginutils": "^3.1.0", - "@types/resolve": "1.17.1", - "builtin-modules": "^3.1.0", - "deepmerge": "^4.2.2", - "is-module": "^1.0.0", - "resolve": "^1.19.0" - } - }, - "@rollup/pluginutils": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-3.1.0.tgz", - "integrity": "sha512-GksZ6pr6TpIjHm8h9lSQ8pi8BE9VeubNT0OMJ3B5uZJ8pz73NPiqOtCog/x2/QzM1ENChPKxMDhiQuRHsqc+lg==", - "dev": true, - "requires": { - "@types/estree": "0.0.39", - "estree-walker": "^1.0.1", - "picomatch": "^2.2.2" - } - }, - "@types/estree": { - "version": "0.0.39", - "resolved": "https://registry.npmjs.org/@types/estree/-/estree-0.0.39.tgz", - "integrity": "sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==", - "dev": true - }, - "@types/node": { - "version": "15.12.4", - "resolved": "https://registry.npmjs.org/@types/node/-/node-15.12.4.tgz", - "integrity": "sha512-zrNj1+yqYF4WskCMOHwN+w9iuD12+dGm0rQ35HLl9/Ouuq52cEtd0CH9qMgrdNmi5ejC1/V7vKEXYubB+65DkA==", - "dev": true - }, "@types/prop-types": { "version": "15.7.3", "resolved": "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.3.tgz", @@ -58,27 +21,12 @@ "csstype": "^3.0.2" } }, - "@types/resolve": { - "version": "1.17.1", - "resolved": "https://registry.npmjs.org/@types/resolve/-/resolve-1.17.1.tgz", - "integrity": "sha512-yy7HuzQhj0dhGpD8RLXSZWEkLsV9ibvxvi6EiJ3bkqLAO1RGo0WbkWQiwpRlSFymTJRz0d3k5LM3kkx8ArDbLw==", - "dev": true, - "requires": { - "@types/node": "*" - } - }, "@types/scheduler": { "version": "0.16.1", "resolved": "https://registry.npmjs.org/@types/scheduler/-/scheduler-0.16.1.tgz", "integrity": "sha512-EaCxbanVeyxDRTQBkdLb3Bvl/HK7PBK6UJjsSixB0iHKoWxE5uu2Q/DgtpOhPIojN0Zl1whvOd7PoHs2P0s5eA==", "dev": true }, - "builtin-modules": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-3.2.0.tgz", - "integrity": "sha512-lGzLKcioL90C7wMczpkY0n/oART3MbBa8R9OFGE1rJxoVI86u4WAGfEk8Wjv10eKSyTHVGkSo3bvBylCEtk7LA==", - "dev": true - }, "commondir": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/commondir/-/commondir-1.0.1.tgz", @@ -91,18 +39,6 @@ "integrity": "sha512-jXKhWqXPmlUeoQnF/EhTtTl4C9SnrxSH/jZUih3jmO6lBKr99rP3/+FmrMj4EFpOXzMtXHAZkd3x0E6h6Fgflw==", "dev": true }, - "deepmerge": { - "version": "4.2.2", - "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.2.2.tgz", - "integrity": "sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg==", - "dev": true - }, - "estree-walker": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-1.0.1.tgz", - "integrity": "sha512-1fMXF3YP4pZZVozF8j/ZLfvnR8NSIljt56UhbZ5PeeDmmGHpgpdwQt7ITlGvYaQukCvuBRMLEiKiYC+oeIg4cg==", - "dev": true - }, "find-cache-dir": { "version": "3.3.1", "resolved": "https://registry.npmjs.org/find-cache-dir/-/find-cache-dir-3.3.1.tgz", @@ -172,12 +108,6 @@ "has": "^1.0.3" } }, - "is-module": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/is-module/-/is-module-1.0.0.tgz", - "integrity": "sha1-Mlj7afeMFNW4FdZkM2tM/7ZEFZE=", - "dev": true - }, "jsonfile": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz", @@ -256,12 +186,6 @@ "find-up": "^4.0.0" } }, - "react-children-utilities": { - "version": "2.5.3", - "resolved": "https://registry.npmjs.org/react-children-utilities/-/react-children-utilities-2.5.3.tgz", - "integrity": "sha512-2qhnQu95KS01ck1+K3lfET1m7Wf6mQsjD7qf1n7lmhCmW3MvmYuLet01Vv1THgZk4E7a963boVHALc0qTlN5gw==", - "dev": true - }, "resolve": { "version": "1.20.0", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.20.0.tgz", diff --git a/package.json b/package.json index f53d15b..733070a 100644 --- a/package.json +++ b/package.json @@ -26,9 +26,7 @@ "react": "17.x.x || 16.x.x || 15.x.x" }, "devDependencies": { - "@rollup/plugin-node-resolve": "^13.0.0", "@types/react": "^17.0.11", - "react-children-utilities": "^2.5.3", "rollup": "^2.52.2", "rollup-plugin-typescript2": "^0.30.0", "tslib": "^2.3.0", diff --git a/rollup.config.js b/rollup.config.js index 6d68aa1..f2dbff7 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,4 +1,3 @@ -import resolve from "@rollup/plugin-node-resolve"; import typescript from "rollup-plugin-typescript2"; import pkg from "./package.json"; @@ -14,6 +13,6 @@ export default { format: "esm", }, ], - plugins: [resolve(), typescript()], + plugins: [typescript()], external: ["react", "react/jsx-runtime"], }; diff --git a/src/index.tsx b/src/index.tsx index 94ebf12..5b75ce6 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,5 +1,4 @@ -import React, { useEffect, useRef } from "react"; -import onlyText from "react-children-utilities/lib/onlyText.js"; +import React, { Children, useEffect, useRef } from "react"; export interface TruncateLinesProps extends React.HTMLAttributes { @@ -54,3 +53,15 @@ function lastIndexWhere( } return start; } + +function onlyText(children: React.ReactNode): string { + let result = ""; + Children.forEach(children, (child) => { + if (typeof child === "number" || typeof child === "string") { + result += child.toString(); + } else { + throw new TypeError("Unexpected child type"); + } + }); + return result; +}