UNPKG

@iicoding/utils

Version:

Browser 端 - 类型判断 - 类似 koa 的异步compose - sleep - 扩展对象属性 - 扩展 storage 对象功能

101 lines (99 loc) 3.03 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/math/cwl.ts var cwl_exports = {}; __export(cwl_exports, { cwlCalculationByScoreAndRemainder: () => cwlCalculationByScoreAndRemainder, cwlKill: () => cwlKill, diffCwl: () => diffCwl }); module.exports = __toCommonJS(cwl_exports); var import_array = require("../array"); var getSum = (arr) => { return arr.reduce((prev, current) => prev + current, 0); }; var cwlKill = () => { }; var cwlCalculationByScoreAndRemainder = (cwl, maxLength = 6, maxNumber = 33) => { if (!Array.isArray(cwl)) { throw "cwl must be an number array"; } const length = cwl.length; if (length < maxLength) { throw `cwl array length must be equal to ${maxLength}`; } if (length > maxLength) { cwl.splice(maxLength, length - 1); } const sum = getSum(cwl); const result = { sum, computed: [], possible: [] }; for (let i = 0; i < length; i++) { const current = cwl[i]; const subtractResult = sum - current; const obtaining = Math.floor(subtractResult / current); const remainder = subtractResult % current; const value = []; if (obtaining > remainder) { if (obtaining > maxNumber) { let flag = obtaining; while (flag > 0) { if (flag <= maxNumber) { value.push(flag); } flag -= 10; } } else { let flag = obtaining; while (flag <= maxNumber) { value.push(flag); flag += 10; } } } else { let flag = obtaining; const flagSum = remainder + obtaining; while (flag <= flagSum) { value.push(flag); flag += 10; } } const possibleValue = value.sort((a, b) => a - b); result.computed.push({ obtaining, remainder, number: current, value: possibleValue }); result.possible.push(...possibleValue); } result.possible = Array.from(new Set(result.possible)).sort((a, b) => a - b); return result; }; var diffCwl = (source, target) => { return (0, import_array.arrayDiff)(cwlCalculationByScoreAndRemainder(source).possible, target); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { cwlCalculationByScoreAndRemainder, cwlKill, diffCwl });