Git Product home page Git Product logo

onex-utils's Introduction

onex-utils


沉淀业务开发过程中的通用工具函数和通用方案,持续进行长期维护和迭代,推荐锁版本使用,欢迎fork和star

📜 文档地址:

https://unity-template.github.io/onex-utils/index.html

✨ 特性

  • 稳定可靠:工具沉淀自高流量业务代码中的业务工具函数
  • 按需加载:提供babelbuild-script插件实现按需加载构建
  • 文档生成:定制TypeDoc支持TypeScript interfacemarkdown 文档生成
  • 持续集成Github Action 持续集成和持续交付(静态扫描、安全扫描、自动测试...)

📦 安装

$ npm install onex-utils --save
$ yarn add onex-utils --save

🔨 使用

import { url } from 'onex-utils';

const url_params_key = url.getUrlParam('key');

⌨️ 开发

  • npm run build: 项目构建命令
  • npm run test: 项目运行单元测试
  • npm run commit: 提交规范化commit
  • npm run lint: 代码格式进行校验
  • npm version patch: 发布正式包

🤝 贡献

  • Github Issue编写features或者Bug
  • fork仓库编写代码然后提交Pull Request

🎯 插件

babel-plugin-onex-utils (babel、webpack)

​查看详情

Install

$ npm i --save onex-utils
$ npm i --save-dev babel-plugin-onex-utils @babel/cli @babel/preset-env

Example

Transforms

import {capitalize, map} from "onex-utils";

map([], capitalize);

roughly to

"use strict";

var _map2 = _interopRequireDefault(require("onex-utils/build/utils/map"));

var _capitalize2 = _interopRequireDefault(
  require("onex-utils/build/utils/capitalize")
);

function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : {default: obj};
}

(0, _map2["default"])([], _capitalize2["default"]);

Usage

.babelrc

{
  "plugins": ["onex-utils"],
  "presets": [["@babel/env", {"targets": {"node": 6}}]]
}

Babel API

require("babel-core").transform("code", {
  plugins: ["onex-utils"],
  presets: [["@babel/env", {targets: {node: 6}}]],
});

webpack.config.js

'module': {
  'loaders': [{
    'loader': 'babel-loader',
    'test': /\.js$/,
    'exclude': /node_modules/,
    'query': {
      'plugins': ['onex-utils'],
      'presets': [['@babel/env', { 'targets': { 'node': 6 } }]]
    }
  }]
}

build-plugin-onex-utils(build-scripts)

查看详情

Install

$ npm install @alib/build-scripts build-plugin-onex-utils build-plugin-component --save-dev

Usage(和 rax 结合使用)

build.json

{
  "type": "rax",
  "targets": ["web"],
  "plugins": ["build-plugin-component", "build-plugin-onex-utils"]
}

package.json

{
  "main": "build/index.js",
  "types": "./lib",
  "files": ["dist", "es", "lib"],
  "scripts": {
    "build": "build-scripts build"
  }
}

cli

$ npm run build

onex-utils's People

Contributors

genluo avatar lihongxun945 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

onex-utils's Issues

添加Flags相关操作

  1. Flags值可以等于2的n次幂
export function removeFlag<T extends number>(flag: T, remove: T & {}): T {
    return ((flag ^ remove) & flag) as T;
}

export function hasAllFlags(flags: number, check: number): boolean {
    return (flags & check) === check;
}

export function hasAnyFlag(flags: number, check: number): boolean {
    return (flags & check) !== 0;
}

// 结合形成一组类型
const commonFlags = ts.SymbolFlags.Transient |
 ts.SymbolFlags.Assignment |
 ts.SymbolFlags.Optional |
 ts.SymbolFlags.Prototype

onex-url中getParams文档错误

复现路径

修改意见

最小仓库

新增用例

针对此bug新增测试用例如下:

  • ................
  • ................
  • ................

文档官网需要进行优化

目前文档内容的呈现存在一定的问题,需要新增定制模板,更好的体现整个工具库的层面关系

添加字体长度装饰器

函数描述

函数接口

测试用例

测试用例应该包含如下:T

  • ................
  • ................
  • ................

dom 中 insertScript 重构

函数描述

  • SCRIPt_DOWNLOAD_TASK -> SCRIPT_DOWNLOAD_TASK 命名修改
  • 类型声明前置
  • element定义可以复用ScriptDomType类型声明
  • 添加参数extOptions 确定 插入的dom容器
  • 脚本加载过程中错误需要使用reject进行暴露
  • 涉及reduce可以切换为some,避免多次dom查询
  • Object.keys 中可以直接使用 object.entries

函数接口

interface InsertOptions {
  type: ScriptType;
  src?: string;
  content?: string;
  extOptions?: Record<string, string>;
  /**
   * 脚本超时等待时间,单位毫秒
   * @defaultValue 3000
   */
  loadTimeout?: number;
  containerNode?: Element;
}


interface InsertScript {
  (options: ): Promise<void>;
}

测试用例

测试用例应该包含如下:T

  • containerNode 参数测试
  • 原有功能测试正常

依赖安装,没有遵循semver version

复现路径

semver -r ^0.12.0 0.12.0 0.13.0 0.13.1

输出0.12.0

修改意见

^指明的版本范围,只要不修改 [major, minor, patch] 三元组中,最左侧的第一个非0位,都是可以的。也就是说,要确定 ^版本包含的范围,先要找到 最左侧的第一个非0位 ,只有在这一位右侧的变动,才被包含在这个 ^ 指定的范围内,需要发布大版本号1.0.0 或者 0.0.1

最小仓库

新增用例

添加条件判断装饰器

函数描述

通过装饰器针对参数GET进行拦截,如果conditions返回为True,执行自定义operation函数

函数接口

type Options<I> = [conditions: (v: I) => boolean, operation: (target: Object, propertyKey: string | symbol) => void];

export interface handleDescriptor<T> {
  (target: Object, propertyKey: string | symbol, options: Options<T>): void;
}

测试用例

测试用例应该包含如下:T

  • 参数校验传入options非函数
  • conditions、operation执行过程中throw error
  • operation中参数是否正确
  • propertyKey为symbol情况

支持文件构建到ES5

复现路径

tsconfig.jsontargte修改为ES5,运行测试命令npm run test,输出

TypeError: Constructor Map requires 'new'

修改意见

支持仓库构建到ES5

生成uuid的函数

函数描述

UUID: 通用唯一标识符 ( Universally Unique Identifier ),对于所有的UUID它可以保证在空间和时间上的唯一性,也称为GUID,全称为:Universally Unique IDentifier ,它是通过MAC地址,时间戳,命名空间,随机数,伪随机数来保证生成ID的唯一性。有着固定的大小( 128 bit位 ),通常由 32 字节的字符串(十六进制)表示。

特性:它的唯一性和一致性特点,使得可以无需注册过程就能够产生一个新的UUID;UUID可以被用作多种用途, 既可以用来短时间内标记一个对象,也可以可靠的辨别网络中的持久性对象。

函数接口

interface GenUUID {
  (): string;
}

测试用例

测试用例应该包含如下:

  • 生成UUID的位数

前端异常处理基类

函数描述

函数接口

测试用例

测试用例应该包含如下:T

  • ................
  • ................
  • ................

【官网优化】工具函数需要有version版本的概念

理想方式

每次发布,动态计算出每个函数的支持版本,不需要通过注释标记:

  1. 发布前需要有一份完整的version和utils的对照文件
  2. 构建文档是依赖当前version和utils的对照文件

目前问题

  1. 发布在线上发布,生成新的version和utils的对照文件无法同步到仓库

解决方式

  1. 提供一个持久化存储的能力,比如通过接口的形式持久化存储数据
  2. 通过钩子每次发布升级的时候,生成这个对照表
  3. 发布文档的同时将对照表保存在一个npm包中,每次发布也将这个npm进行发布

最终解决方式

  1. 使用上述解决方式1中的形式,找到一个持久化存储的地方
  2. 持久化存储为github的gh-pages分支
  3. 构建文档前:通过请求 https://unity-template.github.io/onex-utils/version.map.json ,获取上一次的构建结构
  4. 构建文档过程中:持续update这份数据
  5. 构建文档结束:将这份文件写入到生成的doc中
  6. 使用 git 将构建doc产物推送到远程的gh-pages分支中

地理位置:根据LBS定位计算距离(getDistance)

函数描述

根据LBS定位计算两点之间的距离

函数接口

interface GetDistance {
  (lng1: number, lat1: number, lng2: number, lat2: number): number;
}

测试用例

  • 正确计算两个lbs定位之间的距离
  • 参数为空进行校验

针对服务端数据添加数据校验及其转化

针对服务端数据进行统一校验&转化,主要是针对组件使用过程中进行转化和封装,主要支持以下三种场景

  • 类组件props
  • 函数组件props
  • 服务端数据的转化

使用说明

  1. 类组件的props
export class DataDto {
  @Rule(RuleType.string().required())
  @addPre('test') // 添加前缀
  bizCode: string;

  @Rule(RuleType.string().required())
  @addPre('test') // 添加前缀
  bizName: string;

  @Rule(RuleType.number())
  @addAlarm // 报警装饰
  note?: string;
}

@ValidateComponentProps(DataDto)
class Node extends Component<DataDto> {
  IRender() {
    console.log(this.props.bizCode, this.props.bizName, this.props.note);
  }
}

new Node({
  bizCode: '你好',
  bizName: '世界',
}).IRender();

/**
 * output:
 * Debugger attached.
 * DataDto.note: 参数告警
 * test-你好 test-世界 undefined
 */

2.函数组件的props

export class DataDto {
  @Rule(RuleType.string().required())
  @addPre('test') // 添加前缀
  bizCode: string;

  @Rule(RuleType.string().required())
  @addPre('test') // 添加前缀
  bizName: string;

  @Rule(RuleType.number())
  @addAlarm // 报警装饰
  note?: string;
}

class Node extends Component<DataDto> {
  IRender() {
    console.log(this.props.bizCode, this.props.bizName, this.props.note);
  }
}

const INode = ValidateComponentPropsHoc(DataDto)(Node)

new INode({
  bizCode: '你好',
  bizName: '世界',
}).IRender();

/**
 * output:
 * Debugger attached.
 * DataDto.note: 参数告警
 * test-你好 test-世界 undefined
 */

3.服务端的数据的转化

export class DataDto {
  @Rule(RuleType.string().required())
  @addPre('test') // 添加前缀
  bizCode: string;

  @Rule(RuleType.string().required())
  @addPre('test') // 添加前缀
  bizName: string;

  @Rule(RuleType.number())
  @addAlarm // 报警装饰
  note?: string;
}

const result = validateInterfaceData(DataDto)({
  bizCode: '你好',
  bizName: '世界',
  note: '213',
});

console.log(result.bizCode, result.bizCode, result.note);
// console.log test-你好 test-你好 213

核心理念

  • dto类多次复用
  • 针对不同场景,使用不同的方式进行检验和转化
  • 校验和转化都是鸭子类型进行转化

【官网优化】页面utils需要根据URL进行分组

目前如果需要使用 @category 作为区分的话,有两个问题需要解决

如果支持@category就会丧失文件默认的类型区分(优先走categories对象渲染)-> 单文件中删除categories对象,让其走正常的渲染方式
@category不支持全局的处理,需要编写插件提前处理(遍历children收集全局category) -> 是否可以给Typedoc提交PR

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.