
Security News
GitHub Actions Checkout Now Blocks Risky pull_request_target Checkouts
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.
lin-chuan-function
Advanced tools
日常开发中,有不少常用且通用的js函数,为了方便后续的复用,提高开发效率,所以就搞了一个js函数库 tbl-js-libs;
项目地址: https://github.com/YalongYan/js-libs,
该项目是基于typescript实现的,大家可以基于该项目,或者参考该项目写法再另起一个项目,搭建属于自己的js函数库
不管是vue react, 只要是基于 ES Module 规范都可以使用
简单来说就是 import 加载模块,export 导出模块, 平常基于webpack开发的项目都是基于此规范,所以都可以用
关于js模块化可以看这里: https://www.jianshu.com/p/0494f0e1a507
|_ _ _ dist // tsc编译后的目录
|
|_ _ _ src // 源码
| |
|_ _ _ |_ _ _ array // 数组类的函数集合
| | |
| | |_ _ _arrayUnique.ts // 数组去重函数
| |
| |_ _ _ index.ts // 函数的总入口
| |
| |
|_ _ _ test // mocha 测试代码
| |
| |_ _ _ arrayUnique.test.ts // 单元测试代码
|
|_ _ _ tsconfig.json // typescript 编译配置文件
|
|_ _ _ tsconfig.test.json // 单元测试的配置
/**
* 数组去重
* @param arr {array} 数组
* @returns {array} array
*/
export default function arrayUnique(arr: Array<any>): Array<any> {
return [...new Set(arr)];
}
import 'mocha';
import { expect } from 'chai';
import arrayUnique from '../src/array/arrayUnique';
describe('arrayUnique函数', function(){
it('arrayUnique', function() {
expect(arrayUnique([1,2,2,3,3])).to.deep.equal([1,2,3]);
});
});
"build": "npm run clean && tsc -p tsconfig.json",
"clean": "rimraf ./dist",
"test": "env TS_NODE_PROJECT=\"tsconfig.test.json\" mocha --require ts-node/register test/*.test.ts"
{
"compilerOptions": {
"outDir": "dist",
// esnext 是标准的 es module 格式
"module": "esnext",
// 编译后符合什么es版本
"target": "es5",
// 为每个ts文件 生成一个对应的 .d.ts 文件; 获得 ts 提示
"declaration": true,
"downlevelIteration": true,
"noImplicitAny": true,
"lib":["es5", "dom", "dom.iterable", "es2015", "es2016", "es2017"],
"jsx": "react",
},
"include": [
"src"
],
"exclude": [
"src/**/*.test.tsx",
]
}
{
"compilerOptions": {
"outDir": "dist",
"module": "commonjs", // 就是这个, 运行单元测试的时候 要改成 commonjs 不然会报错
// 编译后符合什么es版本
"target": "es5",
// 为每个ts文件 生成一个对应的 .d.ts 文件; 获得 ts 提示
"declaration": true,
"downlevelIteration": true,
"noImplicitAny": true,
"lib":["es5", "dom", "dom.iterable", "es2015", "es2016", "es2017"],
"jsx": "react",
},
"include": [
"src"
],
"exclude": [
"src/**/*.test.tsx",
]
}
安装依赖: npm i tbl-js-libs -S
import { isEmpty } from 'tbl-js-libs';
export default () => {
console.log(isEmpty([1, 2, 3])) // 这里使用
return (
<div className="homePage">
我是测试页面
</div>
);
};
npm run test 如下报错:
import * as chai from 'chai';
^^^^^^
SyntaxError: Cannot use import statement outside a module
解决方案看 这里
解决方案简单来说,就是新建个 tsconfig.test.json 把里面的 module字段设置为commonjs
这里只是把ts代码编译为es5 的代码,并且采用的是 es module 模式, 如果想把函数库打包为umd 模式的代码,可以使用 webpack 或者 rollup 进行打包
FAQs
We found that lin-chuan-function demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.

Product
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.

Product
Socket MCP now lets AI assistants review org alerts, investigate threats using the Socket threat feed, and inspect package files in addition to dependency scoring.