Переглянути джерело

feat(util): 修改工具包

zhangweicheng 5 роки тому
батько
коміт
8e6345ecc5

mathUtil/.eslintignore → util/.eslintignore


mathUtil/.eslintrc.js → util/.eslintrc.js


mathUtil/.gitignore → util/.gitignore


mathUtil/.huskyrc.js → util/.huskyrc.js


mathUtil/README.md → util/README.md


mathUtil/commitlint.config.js → util/commitlint.config.js


mathUtil/lint-staged.config.js → util/lint-staged.config.js


+ 2 - 1
mathUtil/package.json

@@ -1,5 +1,5 @@
 {
 {
-  "name": "@sc/math",
+  "name": "@sc/util",
   "version": "1.0.0",
   "version": "1.0.0",
   "description": "4舍5入算法,防止浮点数计算不精确问题",
   "description": "4舍5入算法,防止浮点数计算不精确问题",
   "main": "./dist/index.cjs.js",
   "main": "./dist/index.cjs.js",
@@ -21,6 +21,7 @@
     "@commitlint/cli": "^11.0.0",
     "@commitlint/cli": "^11.0.0",
     "@commitlint/config-conventional": "^11.0.0",
     "@commitlint/config-conventional": "^11.0.0",
     "@types/chai": "^4.2.14",
     "@types/chai": "^4.2.14",
+    "@sc/types": "^1.0.10",
     "@types/mocha": "^8.0.4",
     "@types/mocha": "^8.0.4",
     "@types/ms": "^0.7.31",
     "@types/ms": "^0.7.31",
     "@typescript-eslint/eslint-plugin": "^4.4.1",
     "@typescript-eslint/eslint-plugin": "^4.4.1",

mathUtil/prettier.config.js → util/prettier.config.js


mathUtil/rollup.config.js → util/rollup.config.js


+ 34 - 0
util/src/bill.ts

@@ -0,0 +1,34 @@
+import { IJobContent, IItemCharacter } from '@sc/types';
+
+interface IJobsAndCharacterText {
+  jobContentText: string;
+  itemCharacterText: string;
+}
+
+export interface IJobAndCharacter extends IJobsAndCharacterText {
+  jobContents: IJobContent[];
+  itemCharacters: IItemCharacter[];
+}
+
+export const getJobsAndCharacterText = (
+  jobContents: IJobContent[],
+  itemCharacters: IItemCharacter[]
+): IJobsAndCharacterText => {
+  const jobContentText = '';
+  let itemCharacterText = '';
+  const textArray: string[] = ['[项目特征]'];
+  let i = 1;
+  for (const item of itemCharacters) {
+    if (item.isChecked) textArray.push(`${i}. ${item.character}`);
+    i += 1;
+  }
+
+  textArray.push('[工作内容]');
+  i = 1;
+  for (const job of jobContents) {
+    if (job.isChecked) textArray.push(`${i}. ${job.content}`);
+    i += 1;
+  }
+  itemCharacterText = textArray.join('\n');
+  return { jobContentText, itemCharacterText };
+};

+ 1 - 0
util/src/index.ts

@@ -0,0 +1 @@
+export * from './math';

+ 23 - 3
mathUtil/src/index.ts

@@ -1,17 +1,37 @@
+/* eslint-disable no-param-reassign */
 /* eslint-disable no-restricted-globals */
 /* eslint-disable no-restricted-globals */
 // 判断传入的是否是数字或者可转为数字的字符串
 // 判断传入的是否是数字或者可转为数字的字符串
 export const isNumber = (value: string | number): boolean => {
 export const isNumber = (value: string | number): boolean => {
   return /^(-|\+)?\d+(\.\d+)?$/.test(value as string);
   return /^(-|\+)?\d+(\.\d+)?$/.test(value as string);
 };
 };
 
 
+export const innerRound = (
+  num: number,
+  lenght: number,
+  decimal: number
+): number => {
+  let value;
+  let pre = 1;
+  if (lenght === 0) return num;
+  if (num < 0) pre = -1; // 负数的时候先变成正数
+  num *= pre;
+  const n = 10 ** lenght;
+  value = Math.round(num * n);
+  if (lenght <= decimal) {
+    return (value / n) * pre;
+  }
+  value = Math.round(value / 10 ** (lenght - decimal));
+  return (value / 10 ** decimal) * pre;
+};
+
 export const roundForObj = (obj: string | number, decimal: number): number => {
 export const roundForObj = (obj: string | number, decimal: number): number => {
   let value;
   let value;
   if (obj === undefined || obj === null || isNaN(obj as number)) return 0;
   if (obj === undefined || obj === null || isNaN(obj as number)) return 0;
-  const n = 10 ** decimal;
+  const lenght = 10;
   if (obj === +obj) {
   if (obj === +obj) {
-    value = Math.round(obj * n) / n;
+    value = innerRound(obj, lenght, decimal);
   } else {
   } else {
-    value = Math.round(Number(obj) * n) / n;
+    value = innerRound(Number(obj), lenght, decimal);
   }
   }
   return value;
   return value;
 };
 };

mathUtil/tests/test.ts → util/tests/test.ts


+ 1 - 0
mathUtil/tsconfig.json

@@ -6,6 +6,7 @@
     "outDir": "./",
     "outDir": "./",
     "strict": true,
     "strict": true,
     "esModuleInterop": true,
     "esModuleInterop": true,
+    "moduleResolution": "node",
   },
   },
   "include": [
   "include": [
     "src/**/*.ts",
     "src/**/*.ts",