Skip to content

Commit e396e19

Browse files
committed
Merge branch 'develop'
2 parents 466988a + 9b85452 commit e396e19

File tree

11 files changed

+301
-274
lines changed

11 files changed

+301
-274
lines changed

build/tasks/core.js

Lines changed: 1 addition & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
1-
const path = require('path');
2-
const { exec, execSync } = require('child_process');
3-
41
const gulp = require('gulp');
52
const scssimport = require('gulp-shopify-sass');
63
const rename = require('gulp-rename');
74

8-
// const nodeResolver = require('rollup-plugin-node-resolve');
9-
// const cjsResolver = require('rollup-plugin-commonjs');
10-
// const babel = require('rollup-plugin-babel');
11-
// const cleanup = require('rollup-plugin-cleanup');
12-
13-
// const { resolveRollupTask } = require('./functions');
14-
// const { TMP_DIR } = require('./constants');
15-
16-
// const CORE_ROOT = 'src/core';
17-
// const CORE_DIST = 'dist/core';
18-
// const CORE_TMP = `${TMP_DIR}/core`;
19-
205
gulp.task('core-extract-scss', () => {
216
return gulp
227
.src('src/stylesheets/**/*.scss')
@@ -30,92 +15,6 @@ gulp.task('core-extract-scss', () => {
3015
.pipe(gulp.dest('dist/stylesheets'));
3116
});
3217

33-
gulp.task('core-compile-js', () => {
34-
return new Promise((resolve, reject) => {
35-
exec(
36-
'npm run babel:core',
37-
{
38-
cwd: path.resolve(__dirname, '../..'),
39-
maxBuffer: 5 * 1024 * 1024,
40-
},
41-
(err, stdout, stderr) => {
42-
if (err) {
43-
console.error(`exec error: ${err}`);
44-
reject(err);
45-
return;
46-
}
47-
48-
if (stderr) {
49-
console.log(`stderr: ${stderr}`);
50-
}
51-
52-
resolve();
53-
},
54-
).stdout.on('data', function (data) {
55-
console.log(data.toString());
56-
});
57-
});
58-
});
59-
60-
// gulp.task("core-extract-js", ["core-compile-js"], () => {
61-
// const vendorRefs = [
62-
// "calc/index",
63-
// "common/helper",
64-
// "is/index",
65-
// "lbs/index",
66-
// "url/helper",
67-
// "url/index",
68-
// "watermark/index"
69-
// ];
70-
71-
// function resolveDistFile( dn ) {
72-
// return `${CORE_DIST}/utils/${dn}.js`;
73-
// }
74-
75-
// execSync(`rm -rf ${vendorRefs.map(resolveDistFile).join(" ")}`);
76-
77-
// return Promise.all(vendorRefs.map(dn => resolveRollupTask({
78-
// input: `${CORE_ROOT}/utils/${dn}.js`,
79-
// plugins: [
80-
// nodeResolver(),
81-
// babel({
82-
// babelrc: false,
83-
// presets: [["env", {"modules": false}]],
84-
// plugins: ["external-helpers"]
85-
// }),
86-
// cleanup({
87-
// comments: "none",
88-
// maxEmptyLines: 1
89-
// })
90-
// ],
91-
// format: "es",
92-
// file: resolveDistFile(dn),
93-
// name: dn
94-
// })));
95-
// });
96-
97-
// gulp.task('core-extract-js-vendors', ['core-compile-js'], () => {
98-
// return resolveRollupTask({
99-
// input: `${CORE_ROOT}/utils/calc/index.js`,
100-
// plugins: [
101-
// nodeResolver(),
102-
// cjsResolver(),
103-
// babel({
104-
// babelrc: false,
105-
// presets: [['env', { modules: false }]],
106-
// plugins: ['external-helpers'],
107-
// }),
108-
// cleanup({
109-
// comments: 'none',
110-
// maxEmptyLines: 1,
111-
// }),
112-
// ],
113-
// format: 'es',
114-
// file: `${CORE_DIST}/utils/calc/index.js`,
115-
// name: 'calc',
116-
// });
117-
// });
118-
11918
module.exports = {
120-
CORE_TASKS: ['core-extract-scss' /*, 'core-extract-js-vendors'*/],
19+
CORE_TASKS: ['core-extract-scss'],
12120
};

package-lock.json

Lines changed: 1 addition & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
{
22
"name": "handie",
3-
"version": "1.0.10",
3+
"version": "1.0.12",
44
"description": "为前端开发提供统一的布局、组件和工具方法",
5-
"main": "dist/handie.js",
6-
"files": [
7-
"dist"
8-
],
9-
"typings": "dist/typings/index.d.ts",
5+
"main": "handie.esm.js",
6+
"typings": "typings/index.d.ts",
107
"repository": {
118
"type": "git",
129
"url": "[email protected]:ourai/handie.git"
@@ -28,11 +25,10 @@
2825
"prebuild": "rimraf dist",
2926
"build": "tsc && rollup -c rollup.config.ts",
3027
"postbuild": "gulp compile",
28+
"prerelease": "cp package.json *.md dist/",
29+
"release": "cd dist && npm publish",
3130
"start": "rollup -c rollup.config.ts -w"
3231
},
33-
"dependencies": {
34-
"number-precision": "^1.3.1"
35-
},
3632
"devDependencies": {
3733
"@types/jquery": "^3.3.29",
3834
"@types/lodash": "^4.14.149",

rollup.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ const libraryName = 'handie';
1010

1111
export default {
1212
input: `src/index.ts`,
13-
output: [{ file: pkg.main, name: camelCase(libraryName), format: 'es', sourcemap: false }],
13+
output: [
14+
{ file: `dist/${pkg.main.replace('dist/', '')}`, name: camelCase(libraryName), format: 'es', sourcemap: false },
15+
],
1416
// Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
1517
external: [],
1618
watch: {
File renamed without changes.

src/utils/calc/index.ts

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,123 @@
1-
export { plus, minus, times as multiply, divide as divided } from 'number-precision';
1+
// Copied from https://github.com/nefe/number-precision/blob/master/src/index.ts
2+
// for fix TypeScript typing
3+
4+
/**
5+
* @desc 解决浮动运算问题,避免小数点后产生多位数和计算精度损失。
6+
* 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998
7+
*/
8+
9+
/**
10+
* 把错误的数据转正
11+
* strip(0.09999999999999998)=0.1
12+
*/
13+
function strip(num: number, precision = 12): number {
14+
return +parseFloat(num.toPrecision(precision));
15+
}
16+
17+
/**
18+
* Return digits length of a number
19+
* @param {*number} num Input number
20+
*/
21+
function digitLength(num: number): number {
22+
// Get digit length of e
23+
const eSplit = num.toString().split(/[eE]/);
24+
const len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0);
25+
return len > 0 ? len : 0;
26+
}
27+
28+
/**
29+
* 把小数转成整数,支持科学计数法。如果是小数则放大成整数
30+
* @param {*number} num 输入数
31+
*/
32+
function float2Fixed(num: number): number {
33+
if (num.toString().indexOf('e') === -1) {
34+
return Number(num.toString().replace('.', ''));
35+
}
36+
const dLen = digitLength(num);
37+
return dLen > 0 ? strip(num * Math.pow(10, dLen)) : num;
38+
}
39+
40+
/**
41+
* 检测数字是否越界,如果越界给出提示
42+
* @param {*number} num 输入数
43+
*/
44+
function checkBoundary(num: number) {
45+
if (_boundaryCheckingState) {
46+
if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) {
47+
console.warn(`${num} is beyond boundary when transfer to integer, the results may not be accurate`);
48+
}
49+
}
50+
}
51+
52+
/**
53+
* 精确乘法
54+
*/
55+
function times(num1: number, num2: number, ...others: number[]): number {
56+
if (others.length > 0) {
57+
return times(times(num1, num2), others[0], ...others.slice(1));
58+
}
59+
const num1Changed = float2Fixed(num1);
60+
const num2Changed = float2Fixed(num2);
61+
const baseNum = digitLength(num1) + digitLength(num2);
62+
const leftValue = num1Changed * num2Changed;
63+
64+
checkBoundary(leftValue);
65+
66+
return leftValue / Math.pow(10, baseNum);
67+
}
68+
69+
/**
70+
* 精确加法
71+
*/
72+
function plus(num1: number, num2: number, ...others: number[]): number {
73+
if (others.length > 0) {
74+
return plus(plus(num1, num2), others[0], ...others.slice(1));
75+
}
76+
const baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
77+
return (times(num1, baseNum) + times(num2, baseNum)) / baseNum;
78+
}
79+
80+
/**
81+
* 精确减法
82+
*/
83+
function minus(num1: number, num2: number, ...others: number[]): number {
84+
if (others.length > 0) {
85+
return minus(minus(num1, num2), others[0], ...others.slice(1));
86+
}
87+
const baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
88+
return (times(num1, baseNum) - times(num2, baseNum)) / baseNum;
89+
}
90+
91+
/**
92+
* 精确除法
93+
*/
94+
function divide(num1: number, num2: number, ...others: number[]): number {
95+
if (others.length > 0) {
96+
return divide(divide(num1, num2), others[0], ...others.slice(1));
97+
}
98+
const num1Changed = float2Fixed(num1);
99+
const num2Changed = float2Fixed(num2);
100+
checkBoundary(num1Changed);
101+
checkBoundary(num2Changed);
102+
// fix: 类似 10 ** -4 为 0.00009999999999999999,strip 修正
103+
return times(num1Changed / num2Changed, strip(Math.pow(10, digitLength(num2) - digitLength(num1))));
104+
}
105+
106+
/**
107+
* 四舍五入
108+
*/
109+
function round(num: number, ratio: number): number {
110+
const base = Math.pow(10, ratio);
111+
return divide(Math.round(times(num, base)), base);
112+
}
113+
114+
let _boundaryCheckingState = true;
115+
/**
116+
* 是否进行边界检查,默认开启
117+
* @param flag 标记开关,true 为开启,false 为关闭,默认为 true
118+
*/
119+
function enableBoundaryChecking(flag = true) {
120+
_boundaryCheckingState = flag;
121+
}
122+
123+
export { plus, minus, times as multiply, divide as divided };

src/utils/http/helper.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ export function supportJquery(): boolean {
1111
* 判断是否支持 Axios
1212
*/
1313
export function supportAxios(): boolean {
14-
return false;
15-
// return isFunction(axios) && isFunction(axios.get) && isFunction(axios.put) && isFunction(axios.post) && isFunction(axios.delete) && isFunction(axios.options) && isFunction(axios.head) && isFunction(axios.patch) && isFunction(axios.request);
14+
return (
15+
isFunction(axios) &&
16+
isFunction(axios.get) &&
17+
isFunction(axios.put) &&
18+
isFunction(axios.post) &&
19+
isFunction(axios.delete) &&
20+
isFunction(axios.options) &&
21+
isFunction(axios.head) &&
22+
isFunction(axios.patch) &&
23+
isFunction(axios.request)
24+
);
1625
}

0 commit comments

Comments
 (0)