From a2534d3b0bb05d48d0ccf2a9b6cf1d3cba6779c3 Mon Sep 17 00:00:00 2001 From: grantguo <810153274@qq.com> Date: Mon, 12 Aug 2024 17:18:06 +0800 Subject: [PATCH] =?UTF-8?q?add=20yd=5Fmath=5Fmax=20=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/math/max.js | 20 ++++++++++++++++++++ lib/math/max.test.js | 12 ++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 lib/math/max.js create mode 100644 lib/math/max.test.js diff --git a/lib/math/max.js b/lib/math/max.js new file mode 100644 index 0000000..018ec20 --- /dev/null +++ b/lib/math/max.js @@ -0,0 +1,20 @@ +/** + * @description 返回array中最大值,如果为空或假值,则返回undefined + * @author grantguo + * @category math + * @alias yd_math_max + * @param { Array } array + * @return 返回array中最大值 + * @summary 返回array中最大值 + * @example + * yd_math_max([1, 2, 3, 4]); + * // => 4 + * yd_math_max([]); + * // => undefined + * + */ + +export default (array) => { + if (array.length === 0) return undefined; + return Math.max(...array); +}; diff --git a/lib/math/max.test.js b/lib/math/max.test.js new file mode 100644 index 0000000..4f617bd --- /dev/null +++ b/lib/math/max.test.js @@ -0,0 +1,12 @@ +import { describe, it, expect } from 'vitest'; +import yd_math_max from './max.js'; + +describe('yd_math_max', () => { + it('it shoule return a number', () => { + const arr = [1, 2, 3, 4]; + const number_1 = yd_math_max(arr); + expect(number_1).toEqual(4); + const number_2 = yd_math_max([]); + expect(number_2).toEqual(undefined); + }); +});