增加 money 和 mask

This commit is contained in:
Wang Kejun 2023-03-01 12:45:04 +08:00
parent 42fca4aa3f
commit 02f253f591
12 changed files with 147 additions and 42 deletions

View File

@ -1,5 +1,6 @@
import { RecurrenceRule, RecurrenceSpecDateRange, RecurrenceSpecObjLit } from 'node-schedule';
import { EntityDict } from './Entity';
import { AsyncContext } from "../store/AsyncRowStore";
import { AsyncContext } from '../store/AsyncRowStore';
declare type RoutineFn<ED extends EntityDict, Cxt extends AsyncContext<ED>> = (context: Cxt) => Promise<string>;
export declare type Routine<ED extends EntityDict, Cxt extends AsyncContext<ED>> = {
name: string;
@ -7,7 +8,7 @@ export declare type Routine<ED extends EntityDict, Cxt extends AsyncContext<ED>>
};
export declare type Timer<ED extends EntityDict, Cxt extends AsyncContext<ED>> = {
name: string;
cron: string;
cron: RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date | string | number;
fn: RoutineFn<ED, Cxt>;
};
export {};

1
lib/utils/cron.d.ts vendored
View File

@ -1 +0,0 @@
export declare function schedule(cron: string, fn: (date: Date) => any): void;

View File

@ -1,18 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.schedule = void 0;
var tslib_1 = require("tslib");
var cronjs_matcher_1 = require("@datasert/cronjs-matcher");
var dayjs_1 = tslib_1.__importDefault(require("dayjs"));
function schedule(cron, fn) {
var futureMatches = (0, cronjs_matcher_1.getFutureMatches)(cron, {
matchCount: 1,
});
var date = (0, dayjs_1.default)(futureMatches[0]);
var interval = date.diff((0, dayjs_1.default)(), 'ms');
setTimeout(function () {
fn(new Date());
schedule(cron, fn);
}, interval);
}
exports.schedule = schedule;

5
lib/utils/mask.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
declare const maskIdCard: (idCardNumber: string) => string;
declare const maskMobile: (mobile: string) => string;
declare const maskName: (name: string) => string;
declare const maskStar: (str: string, front: number, end: number, star: string) => string;
export { maskIdCard, maskMobile, maskName, maskStar, };

35
lib/utils/mask.js Normal file
View File

@ -0,0 +1,35 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.maskStar = exports.maskName = exports.maskMobile = exports.maskIdCard = void 0;
var maskIdCard = function (idCardNumber) {
if (!idCardNumber instanceof String) {
throw new Error("身份证号码必须是String类型");
}
var begin = idCardNumber.slice(0, 4);
var end = idCardNumber.slice(idCardNumber.length - 4, 4);
for (var i = 0; i < idCardNumber.length - 8; i++) {
begin = begin.concat("*");
}
return begin.concat(end);
};
exports.maskIdCard = maskIdCard;
var maskMobile = function (mobile) {
var begin = mobile.slice(0, 3);
var end = mobile.slice(7, 11);
return begin.concat("****").concat(end);
};
exports.maskMobile = maskMobile;
var maskName = function (name) {
return name.slice(0, name.length - 1).concat("*");
};
exports.maskName = maskName;
var maskStar = function (str, frontLen, endLen, star) {
if (star === void 0) { star = '*'; }
var len = str.length - frontLen - endLen;
var xing = '';
for (var i = 0; i < len; i++) {
xing += star;
}
return str.substring(0, frontLen) + xing + str.substring(str.length - endLen);
};
exports.maskStar = maskStar;

5
lib/utils/money.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
declare const ToCent: (float: number) => number;
declare const ToYuan: (int: number) => number;
declare const StringToCent: (value: string, allowNegative?: true) => number | undefined;
declare const CentToString: (value: number) => string | undefined;
export { ToCent, ToYuan, StringToCent, CentToString, };

24
lib/utils/money.js Normal file
View File

@ -0,0 +1,24 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CentToString = exports.StringToCent = exports.ToYuan = exports.ToCent = void 0;
var ToCent = function (float) {
return Math.round(float * 100);
};
exports.ToCent = ToCent;
var ToYuan = function (int) {
return Math.round(int) / 100;
};
exports.ToYuan = ToYuan;
var StringToCent = function (value, allowNegative) {
var numValue = parseInt(value, 10);
if (typeof numValue === 'number' && (numValue >= 0 || allowNegative)) {
return ToCent(numValue);
}
};
exports.StringToCent = StringToCent;
var CentToString = function (value) {
if (typeof value === 'number') {
return "".concat(ToYuan(value));
}
};
exports.CentToString = CentToString;

View File

@ -27,6 +27,7 @@
"@types/luxon": "^2.0.9",
"@types/mocha": "^8.2.0",
"@types/node": "^14.14.25",
"@types/node-schedule": "^2.1.0",
"@types/react": "^17.0.2",
"@types/uuid": "^8.3.0",
"@types/wechat-miniprogram": "^3.4.1",
@ -40,9 +41,9 @@
"typescript": "^4.7.4"
},
"dependencies": {
"@datasert/cronjs-matcher": "^1.2.0",
"dayjs": "^1.11.5",
"lodash": "^4.17.21",
"node-schedule": "^2.1.1",
"uuid": "^9.0.0"
}
}

View File

@ -1,7 +1,10 @@
import { RecurrenceRule, RecurrenceSpecDateRange, RecurrenceSpecObjLit } from 'node-schedule';
import { EntityDict } from './Entity';
import { AsyncContext } from "../store/AsyncRowStore";
import { AsyncContext } from '../store/AsyncRowStore';
type RoutineFn<ED extends EntityDict, Cxt extends AsyncContext<ED>> = (context: Cxt) => Promise<string>;
type RoutineFn<ED extends EntityDict, Cxt extends AsyncContext<ED>> = (
context: Cxt
) => Promise<string>;
export type Routine<ED extends EntityDict, Cxt extends AsyncContext<ED>> = {
name: string;
@ -10,6 +13,6 @@ export type Routine<ED extends EntityDict, Cxt extends AsyncContext<ED>> = {
export type Timer<ED extends EntityDict, Cxt extends AsyncContext<ED>> = {
name: string;
cron: string;
cron: RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date | string | number;
fn: RoutineFn<ED, Cxt>;
};

View File

@ -1,17 +0,0 @@
import { getFutureMatches } from '@datasert/cronjs-matcher';
import DayJs from 'dayjs';
export function schedule(cron: string, fn: (date: Date) => any) {
const futureMatches = getFutureMatches(cron, {
matchCount: 1,
});
const date = DayJs(futureMatches[0]);
const interval = date.diff(DayJs(), 'ms');
setTimeout(
() => {
fn(new Date());
schedule(cron, fn);
},
interval
);
}

40
src/utils/mask.ts Normal file
View File

@ -0,0 +1,40 @@
const maskIdCard: (idCardNumber: string) => string = (idCardNumber) => {
if(!idCardNumber as any instanceof String) {
throw new Error("身份证号码必须是String类型");
}
let begin = idCardNumber.slice(0, 4);
let end = idCardNumber.slice(idCardNumber.length - 4, 4);
for(let i = 0; i < idCardNumber.length - 8; i ++) {
begin = begin.concat("*");
}
return begin.concat(end);
}
const maskMobile: (mobile: string) => string = (mobile) => {
let begin = mobile.slice(0, 3);
let end = mobile.slice(7, 11);
return begin.concat("****").concat(end);
}
const maskName: (name: string) => string = (name) => {
return name.slice(0, name.length - 1).concat("*");
}
const maskStar: (str: string, front: number, end: number, star: string) => string = (str, frontLen, endLen, star = '*') => {
const len = str.length - frontLen - endLen;
let xing = '';
for (let i = 0; i < len; i++) {
xing += star;
}
return str.substring(0, frontLen) + xing + str.substring(str.length - endLen);
}
export {
maskIdCard,
maskMobile,
maskName,
maskStar,
}

27
src/utils/money.ts Normal file
View File

@ -0,0 +1,27 @@
const ToCent: (float: number) => number = (float) => {
return Math.round(float * 100);
}
const ToYuan: (int: number) => number = ( int) => {
return Math.round(int) / 100;
}
const StringToCent: (value: string, allowNegative?: true) => number | undefined = (value, allowNegative) => {
const numValue = parseInt(value, 10);
if (typeof numValue === 'number' && (numValue >= 0 || allowNegative)) {
return ToCent(numValue);
}
}
const CentToString: (value: number) => string | undefined = (value) => {
if (typeof value === 'number') {
return `${ToYuan(value)}`;
}
}
export {
ToCent,
ToYuan,
StringToCent,
CentToString,
}