# monose-utils **Repository Path**: monoseven/monose-utils ## Basic Information - **Project Name**: monose-utils - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-01-03 - **Last Updated**: 2025-08-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 本项目包含了一些项目中高频使用或者工具类的方法,如: 1. 各个项目中通用的工具方法,例如导出图片、PDF、表格、根据word模板生成文档等; 2. 高频使用的函数:例如格式化数据、平铺数据转树状结构等; 3. 时间处理; 4. storage封装; # 快速开始 1. 安装 ```bash npm install monose-utils ``` 2. 引入并使用 ```ts import { generateTimeArray } from 'monose-utils'; const timeArray = generateTimeArray(24, 'end'); console.log(timeArray); // ['00:15', '01:15', ..., '23:15', '24:00'] ``` # 目录 - [使用文档](#使用文档) - [file](#file) - [checkFileType](#checkfiletype) - [downloadFileByUrl](#downloadfilebyurl) - [downloadFileByBlob](#downloadfilebyblob) - [downloadFileResponse](#downloadfileresponse) - [exportToImage](#exporttoimage) - [exportToPDF](#exporttopdf) - [convertHtmlToImageModule](#converthtmltoimagemodule) - [readFileAsArrayBuffer](#readfileasarraybuffer) - [exportToWord](#exporttoword) - [exportToExcel](#exporttoexcel) - [handleMultiLevelHeaders](#handlemultilevelheaders) - [format](#format) - [toFixedByKeys](#tofixedbykeys) - [formMapToOption](#formmaptooption) - [formOptionToMap](#formOptionToMap) - [clearEmptyFields](#clearemptyfields) - [modifyKeyNameForRecordList](#modifykeynameforrecordlist) - [dealTileToTree](#dealtiletotree) - [dealTileToTreeWithoutHead](#dealtiletotreewithouthead) - [dfsSort](#dfssort) - [findNodeByPropertyValue](#findnodebypropertyvalue) - [echarts](#echarts) - [getEchartsTooltipMarker](#getechartstooltipmarker) - [getEchartsTooltipContent](#getechartstooltipcontent) - [WebStorage](#webstorage) - [set](#set) - [get](#get) - [getAll](#getall) - [remove](#remove) - [clear](#clear) - [useLocalStorage](#uselocalstorage) - [useSessionStorage](#usesessionstorage) - [timeTools](#timetools) - [generateTimeArray](#generatetimearray) - [getFirstAndLastDayOfWeek](#getfirstandlastdayofweek) - [getFirstAndLastDayOfWeekWithoutNext](#getfirstandlastdayofweekwithoutnext) - [getFirstAndLastDayOfMonth](#getfirstandlastdayofmonth) - [getYearsInRange](#getyearsinrange) - [getDatesInRange](#getdatesinrange) - [getMonthsInRange](#getmonthsinrange) - [getMonthsByDates](#getmonthsbydates) - [getDatesInMonths](#getdatesinmonths) - [getMonthsInYears](#getmonthsinyears) - [getMonthsInYearsWithoutNext](#getmonthsinyearswithoutnext) - [getDatesInMonthsWithoutNext](#getdatesinmonthswithoutnext) - [convertSecondsToTime](#convertsecondstotime) # 使用文档 ## file ### checkFileType 检查文件类型是否匹配指定的类型列表。 **说明**: 该方法用于检查文件类型是否匹配指定的类型列表。例如,当传入的文件名为 'example.txt',类型列表为 ['.txt', '.md'] 时,返回 true。 **参数**: - `fileName` (string): 文件名。 - `typeList` (string[]): 类型列表。 **返回值**: - `boolean`: 返回是否匹配。 **使用示例**: ```typescript const isMatch = checkFileType('example.txt', ['.txt', '.md']); console.log(isMatch); // true ``` ### downloadFileByUrl 通过URL下载文件。 **说明**: 该方法用于通过 URL 下载文件。例如,当调用 downloadFileByUrl('https://example.com/file.txt', 'example.txt') 时,会下载文件并命名为 'example.txt'。 **参数**: - `url` (string): 文件URL。 - `fileName` (string, 可选): 文件名。 **返回值**: 无 **使用示例**: ```typescript downloadFileByUrl('https://example.com/file.txt', 'example.txt'); ``` ### downloadFileByBlob 通过 Blob 下载文件。 **说明**: 该方法用于通过 Blob 下载文件。例如,当调用 downloadFileByBlob(new Blob(), 'example.txt') 时,会下载文件并命名为 'example.txt'。 **参数**: - `blob` (Blob): 文件Blob。 - `fileName` (string, 可选): 文件名。 **返回值**: 无 **使用示例**: ```typescript downloadFileByBlob(new Blob(), 'example.txt'); ``` ### downloadFileResponse 下载文件响应数据。 **说明**: 该方法用于下载文件响应数据。例如,当调用 downloadFileResponse({ file: new Blob(), fileName: 'example.txt' }) 时,会下载文件并命名为 'example.txt'。 **参数**: - `data` (IFileResponse): 文件响应数据。 - `customFileName` (string, 可选): 自定义文件名,默认值为 '未设置文件名'。 **返回值**: 无 **使用示例**: ```typescript const fileResponse = { file: new Blob(), fileName: 'example.txt' }; downloadFileResponse(fileResponse); ``` ### exportToImage 导出 HTML 元素为图片。 **说明**: 该方法用于将 HTML 元素导出为图片。例如,当调用 exportToImage(document.body, 'screenshot') 时,会将页面导出为名为 'screenshot.png' 的图片。 **参数**: - `element` (HTMLElement): HTML元素。 - `fileName` (string, 可选): 文件名,默认值为 'screenshot'。 - `html2canvasOption` (Partial, 可选): html2canvas选项。 **返回值**: 无 **使用示例**: ```typescript exportToImage(document.body, 'screenshot'); ``` ### exportToPDF 导出 HTML 元素为 PDF 文件。 **说明**: 该方法用于将 HTML 元素导出为 PDF 文件。例如,当调用 exportToPDF(document.body, 'download') 时,会将页面导出为名为 'download.pdf' 的 PDF 文件。 **参数**: - `element` (HTMLElement): HTML元素。 - `fileName` (string, 可选): 文件名,默认值为 'download'。 - `html2canvasOption` (Partial, 可选): html2canvas选项。 - `jspdfOption` (jsPDFOptions, 可选): jsPDF选项。 **返回值**: 无 **使用示例**: ```typescript exportToPDF(document.body, 'download'); ``` ### convertHtmlToImageModule 将 HTML元素转换为 Word 导出所需的图像模块,主要服务于 exportToWord 方法。 **说明**: 该方法用于将 HTML 元素转换为 Word 导出所需的图像模块。例如,当调用 convertHtmlToImageModule(document.body) 时,会返回包含图像模块参数的 Promise。 **参数**: - `element` (HTMLElement): HTML元素。 - `imageModuleSize` (Object, 可选): 图像模块大小。 - `width` (number, 可选): 宽度。 - `height` (number, 可选): 高度。 - `html2canvasOption` (Partial, 可选): html2canvas选项。 **返回值**: - `Promise`: 返回图像模块参数的Promise。 **使用示例**: ```typescript convertHtmlToImageModule(document.body).then((imageModule) => { console.log(imageModule); }); ``` ### readFileAsArrayBuffer 读取文件为 ArrayBuffer。 **说明**: 该方法用于读取文件为 ArrayBuffer。例如,当调用 readFileAsArrayBuffer(new Blob()) 时,会返回包含 ArrayBuffer 的 Promise。 **参数**: - `blob` (Blob): 文件Blob。 **返回值**: - `Promise`: 返回ArrayBuffer的Promise。 **使用示例**: ```typescript const blob = new Blob(); readFileAsArrayBuffer(blob).then((arrayBuffer) => { console.log(arrayBuffer); }); ``` ### exportToWord 将数据导出为 Word 文档,可包含图片,具体模板的创建方式,请参照 `docxtemplater`。 **说明**: 该方法用于将数据导出为 Word 文档。例如,当调用 exportToWord('template.docx', { name: 'John' }) 时,会将数据填充到模板并导出为 Word 文档。 **参数**: - `templatePath` (string): 模板路径。 - `data` (Record): 数据。 - `fileName` (string, 可选): 文件名。 **返回值**: 无 **使用示例**: ```typescript // 模板中,文字使用 {name} 占位符,图片使用 {%image} 占位符 exportToWord('template.docx', { name: 'John', image: await convertHtmlToImageModule(element) }); ``` ### exportToExcel 将数据导出为 Excel 文件。 **说明**: 该方法用于将数据导出为 Excel 文件。例如,当调用 exportToExcel({ fileName: 'data.xlsx', column: [], data: [] }) 时,会将数据导出为名为 'data.xlsx' 的 Excel 文件。 **参数**: - `option` (SheetOption[] | SheetOption): 工作表配置选项。 - `sheetName` (string): 可选,sheet页名称。 - `column` (Partial[]): 列配置,定义每列的属性(如表头、宽度等)。 - `data` (Record[]): 要导出的数据数组。 - `multiLevelHeaders` ((string | undefined)[][], 可选): 多级表头配置,二维数组形式。 - `mergeCellList` ([number, number, number, number][], 可选): 需要合并的单元格列表,每项为 [startRow, startCol, endRow, endCol]。 - `fileName` (string, 可选): 文件名。 **返回值**: 无 **使用示例**: ```typescript // 单表导出 exportToExcel( { column: [ { header: '姓名', key: 'name' }, { header: '年龄', key: 'age' }, ], data: [ { name: '张三', age: 18 }, { name: '李四', age: 20 }, ], }, '人员名单', ); // 多表导出 exportToExcel( [ { sheetName: '人员表', column: [ { header: '姓名', key: 'name' }, { header: '年龄', key: 'age' }, ], data: [{ name: '张三', age: 18 }], }, { sheetName: '部门表', column: [ { header: '部门名称', key: 'dept' }, { header: '人数', key: 'count' }, ], data: [{ dept: '技术部', count: 10 }], }, ], '公司数据', ); // 多级表头 exportToExcel( { column: [{ key: 'name' }, { key: 'age' }], multiLevelHeaders: [ ['基本信息', undefined], ['姓名', '年龄'], ], data: [{ name: '张三', age: 18 }], }, '学生信息表', ); // 合并单元格 exportToExcel( { column: [ { header: '姓名', key: 'name' }, { header: '成绩', key: 'score' }, ], data: [ { name: '张三', score: 90 }, { name: '李四', score: 85 }, ], mergeCellList: [ [1, 1, 2, 1], // 合并第1列的1-2行 ], }, '成绩单', ); ``` ### handleMultiLevelHeaders 处理多级表头。 **说明**: 该方法用于处理多级表头。例如,当调用 handleMultiLevelHeaders(worksheet, [['Header1', 'Header2']]) 时,会处理多级表头。 **参数**: - `worksheet` (Worksheet): 工作表。 - `headers` ((string | undefined)[][]): 表头。 **返回值**: 无 **使用示例**: ```typescript const worksheet = new Worksheet(); handleMultiLevelHeaders(worksheet, [['Header1', 'Header2']]); ``` ## format ### toFixedByKeys 将对象中的指定键的值格式化为指定的小数位数。 **说明**: 该方法用于将对象中的指定键的值格式化为指定的小数位数。例如,当传入的对象为 { a: 1.2345, b: 2.3456 },键数组为 ['a', 'b'],小数位数为 2 时,返回的对象为 { a: '1.23', b: '2.35' }。 **参数**: - `data` (T): 要格式化的对象。 - `keys` (Array): 要格式化的键数组。 - `fractionDigits` (number, 可选): 小数位数,默认为2。 **返回值**: - `T`: 返回格式化后的对象。 **使用示例**: ```typescript const data = { a: 1.2345, b: 2.3456 }; const formattedData = toFixedByKeys(data, ['a', 'b'], 2); console.log(formattedData); // { a: '1.23', b: '2.35' } ``` ### formMapToOption 将Map对象转换为选项数组。 **说明**: 该方法用于将 Map 对象转换为选项数组。例如,当传入的 Map 对象为 new Map([['a', '选项A'], ['b', '选项B']]) 时,返回的数组为 [{ label: '选项A', value: 'a' }, { label: '选项B', value: 'b' }]。 **参数**: - `sheet` (Map): 要转换的Map对象。 **返回值**: - `Array<{label: V, value: K}>`: 返回转换后的选项数组。 **使用示例**: ```typescript const map = new Map([ ['a', '选项A'], ['b', '选项B'], ]); const options = formMapToOption(map); console.log(options); // [{ label: '选项A', value: 'a' }, { label: '选项B', value: 'b' }] ``` ### formOptionToMap 将选项数组转换为Map对象。 **说明**: 该方法用于将选项数组转换为Map对象。例如,当传入的选项数组为 [{ label: '选项A', value: 'a' }, { label: '选项B', value: 'b' }] 时,返回的Map对象为 new Map([['a', '选项A'], ['b', '选项B']])。 **参数**: - `options` (Array<{ label: V, value: K }>): 要转换的选项数组。 **返回值**: - `Map`: 返回转换后的Map对象。 **使用示例**: ```typescript const options = [ { label: '选项A', value: 'a' }, { label: '选项B', value: 'b' }, ]; const map = formOptionToMap(options); console.log(map); // Map { 'a' => '选项A', 'b' => '选项B' } ``` ### clearEmptyFields 清除对象中的空字段。 **说明**: 该方法用于清除对象中的空字段。例如,当传入的对象为 { a: 1, b: '', c: 2 } 时,返回的对象为 { a: 1, c: 2 }。 **参数**: - `data` (T): 要清除空字段的对象。 **返回值**: - `T`: 返回清除空字段后的对象。 **使用示例**: ```typescript const data = { a: 1, b: '', c: 2 }; const cleanedData = clearEmptyFields(data); console.log(cleanedData); // { a: 1, c: 2 } ``` ### modifyKeyNameForRecordList 将一个对象数组中的每一项的键名通过指定的关系转换成其他的键名。 **说明**: 该方法用于将一个对象数组中的每一项的键名通过指定的关系转换成其他的键名。例如,当传入的记录列表为 [{ a: 1, b: 2 }],键名映射表为 { a: 'A', b: 'B' } 时,返回的记录列表为 [{ A: 1, B: 2 }]。 **参数**: - `data` (Array>): 要修改的记录列表。 - `sheet` (Record): 键名映射表。 **返回值**: - `Array>`: 返回修改后的记录列表。 **使用示例**: ```typescript const data = [{ a: 1, b: 2 }]; const sheet = { a: 'A', b: 'B' }; const modifiedData = modifyKeyNameForRecordList(data, sheet); console.log(modifiedData); // [{ A: 1, B: 2 }] ``` ### dealTileToTree 将平铺的数据转换为树结构。 **说明**: 该方法用于将平铺的数据转换为树结构。例如,当传入的平铺数据为 [{ id: 1, parentId: null }, { id: 2, parentId: 1 }],标识字段为 'id',父标识字段为 'parentId',头部节点数组为 [null] 时,返回的树结构数据为 [{ id: 1, parentId: null, children: [{ id: 2, parentId: 1, children: [] }] }] **参数**: - `list` (Array): 要转换的平铺数据。 - `flag` (keyof T): 标识字段。 - `pFlag` (keyof T): 父标识字段。 - `heads` (Array): 头部节点数组。 **返回值**: - `Array }>`: 返回树结构数据。 **使用示例**: ```typescript const list = [ { id: 1, parentId: null }, { id: 2, parentId: 1 }, ]; const tree = dealTileToTree(list, 'id', 'parentId', [null]); console.log(tree); // [{ id: 1, parentId: null, children: [{ id: 2, parentId: 1, children: [] }] }] ``` ### dealTileToTreeWithoutHead 将平铺的数据转换为树结构(无头部节点)。 **说明**: 该方法用于将平铺的数据转换为树结构,自动识别头部节点。例如,当传入的平铺数据为 [{ id: 1, parentId: null }, { id: 2, parentId: 1 }],标识字段为 'id',父标识字段为 'parentId' 时,返回的树结构数据为 [{ id: 1, parentId: null, children: [{ id: 2, parentId: 1, children: [] }] }] **参数**: - `list` (Array): 要转换的平铺数据。 - `flag` (keyof T): 标识字段。 - `pFlag` (keyof T): 父标识字段。 **返回值**: - `Array }>`: 返回树结构数据。 **使用示例**: ```typescript const list = [ { id: 1, parentId: null }, { id: 2, parentId: 1 }, ]; const tree = dealTileToTreeWithoutHead(list, 'id', 'parentId'); console.log(tree); // [{ id: 1, parentId: null, children: [{ id: 2, parentId: 1, children: [] }] }] ``` ### dfsSort 深度优先排序。 **说明**: 该方法用于对树结构数据进行深度优先排序。例如,当传入的树结构数据为 [{ id: 2, children: [] }, { id: 1, children: [] }],排序字段为 'id' 时,返回的排序后数组为 [{ id: 1, children: [] }, { id: 2, children: [] }] **参数**: - `list` (Array): 要排序的数组。 - `flag` (keyof T): 排序字段。 **返回值**: - `Array`: 返回排序后的数组。 **使用示例**: ```typescript const list = [ { id: 2, children: [] }, { id: 1, children: [] }, ]; const sortedList = dfsSort(list, 'id'); console.log(sortedList); // [{ id: 1, children: [] }, { id: 2, children: [] }] ``` ### findNodeByPropertyValue 根据属性值查找节点。 **说明**: 该方法用于在树结构数据中根据属性值查找节点。例如,当传入的树结构数据为 [{ id: 1, children: [{ id: 2, children: [] }] }],属性名为 'id',属性值为 2 时,返回的节点为 { id: 2, children: [] } **参数**: - `tree` (Array): 要查找的树结构数据。 - `keyName` (keyof T): 属性名。 - `keyValue` (any): 属性值。 **返回值**: - `T | null`: 返回找到的节点或null。 **使用示例**: ```typescript const tree = [{ id: 1, children: [{ id: 2, children: [] }] }]; const node = findNodeByPropertyValue(tree, 'id', 2); console.log(node); // { id: 2, children: [] } ``` ## echarts ### getEchartsTooltipMarker 获取 ECharts 提示框的标记。 **说明**: 该方法用于获取 ECharts 提示框的标记 HTML 字符串。例如,当传入的颜色为 'red' 时,返回的 HTML 字符串为 ``。 **参数**: - `color` (string): 标记颜色。 **返回值**: - `string`: 返回 HTML 字符串。 **使用示例**: ```typescript const marker = getEchartsTooltipMarker('red'); console.log(marker); // ``` ### getEchartsTooltipContent 获取 ECharts 提示框的内容。 **说明**: 该方法用于获取 ECharts 提示框的内容 HTML 字符串。例如,当传入的数据列表为 [['Label1', 'Value1'], ['Label2', 'Value2']] 时,返回的 HTML 字符串为 `
Label1
Value1
Label2
Value2
`。 **参数**: - `dataList` (Array<[string, string | undefined | number] | string>): 数据列表。 **返回值**: - `string`: 返回 HTML 字符串。 **使用示例**: ```typescript const content = getEchartsTooltipContent([ ['Label1', 'Value1'], ['Label2', 'Value2'], ]); console.log(content); //
Label1
Value1
Label2
Value2
``` ## WebStorage 对 local storage 和 session storage 的封装 ### set 设置本地存储项。 **说明**: 该方法用于设置本地存储项。可以指定存储项的键、值和过期时间。例如,当调用 set('token', 'abc123', 3600) 时,会在本地存储中保存一个键为 'token',值为 'abc123',过期时间为 1 小时的存储项。 **参数**: - `key` (string): 存储项的键。 - `value` (any): 存储项的值。 - `expire` (number | null): 过期时间(秒)。 **返回值**: 无 **使用示例**: ```typescript const storage = new WebStorage(); storage.set('token', 'abc123', 3600); ``` ### get 获取本地存储项。 **说明**: 该方法用于获取本地存储项的值。如果存储项已过期或不存在,则返回 null。例如,当调用 get('token') 时,会返回键为 'token' 的存储项的值。 **参数**: - `key` (string): 存储项的键。 **返回值**: - `any`: 返回存储项的值。 **使用示例**: ```typescript const storage = new WebStorage(); const token = storage.get('token'); console.log(token); // 'abc123' ``` ### getAll 获取所有本地存储项。 **说明**: 该方法用于获取所有本地存储项的键值对。例如,当调用 getAll() 时,会返回一个包含所有存储项的对象。 **参数**: 无 **返回值**: - `Record`: 返回所有存储项的键值对。 **使用示示例**: ```typescript const storage = new WebStorage(); const allItems = storage.getAll(); console.log(allItems); ``` ### remove 删除本地存储项。 **说明**: 该方法用于删除指定键的本地存储项。例如,当调用 remove('token') 时,会删除键为 'token' 的存储项。 **参数**: - `key` (string): 存储项的键。 **返回值**: 无 **使用示例**: ```typescript const storage = new WebStorage(); storage.remove('token'); ``` ### clear 清空所有本地存储项。 **说明**: 该方法用于清空所有本地存储项。例如,当调用 clear() 时,会清空本地存储中的所有存储项。 **参数**: 无 **返回值**: 无 **使用示例**: ```typescript const storage = new WebStorage(); storage.clear(); ``` ### useLocalStorage 使用 localStorage 创建 WebStorage 实例。 **说明**: 该方法用于创建一个使用 localStorage 的 WebStorage 实例。例如,当调用 useLocalStorage() 时,会返回一个使用 localStorage 的 WebStorage 实例。 **参数**: 无 **返回值**: - `WebStorage`: 返回使用 localStorage 的 WebStorage 实例。 **使用示例**: ```typescript const localStorageInstance = useLocalStorage(); localStorageInstance.set('key', 'value'); ``` ### useSessionStorage 使用 sessionStorage 创建 WebStorage 实例。 **说明**: 该方法用于创建一个使用 sessionStorage 的 WebStorage 实例。例如,当调用 useSessionStorage() 时,会返回一个使用 sessionStorage 的 WebStorage 实例。 **参数**: 无 **返回值**: - `WebStorage`: 返回使用 sessionStorage 的 WebStorage 实例。 **使用示例**: ```typescript const sessionStorageInstance = useSessionStorage(); sessionStorageInstance.set('key', 'value'); ``` ## timeTools ### generateTimeArray 生成时间数组。 **说明**: 该方法用于生成一天内的时间点数组。根据传入的时间间隔和模式,生成从 00:00 到 24:00 的时间点。例如,当 interval 为 24 且 mode 为 'end' 时,生成的时间数组为 ['00:15', '01:15', ..., '23:15', '24:00']。 **参数**: - `interval` (24 | 96): 时间间隔,24 表示每小时一个时间点,96 表示每 15 分钟一个时间点。 - `mode` ('start' | 'end'): 模式,'start' 表示从 00:00 开始,'end' 表示从 00:15 开始。 **返回值**: - `string[]`: 返回生成的时间数组。 **使用示例**: ```typescript const timeArray = generateTimeArray(24, 'end'); console.log(timeArray); // ['00:15', '01:15', ..., '23:15', '24:00'] ``` ### getFirstAndLastDayOfWeek 获取指定日期所在周的第一天和最后一天。 **说明**: 该方法用于获取指定日期所在周的第一天(周一)和最后一天(周日)的日期。例如,当传入的日期为 '2023-01-04' 时,返回的数组为 ['2023-01-02', '2023-01-08']。 **参数**: - `date` (Date | number | string, 可选): 指定日期,默认为当前日期。 **返回值**: - `[string, string]`: 返回一个包含第一天和最后一天的数组,格式为 'YYYY-MM-DD'。 **使用示例**: ```typescript const [firstDay, lastDay] = getFirstAndLastDayOfWeek('2023-01-04'); console.log(firstDay, lastDay); // '2023-01-02', '2023-01-08' ``` ### getFirstAndLastDayOfWeekWithoutNext 获取指定日期所在周的第一天和目标天。 **说明**: 该方法用于获取指定日期所在周的第一天(周一)和目标天的日期。目标天数偏移量用于指定目标天相对于当前日期的偏移量。例如,当传入的日期为 '2023-01-04' 且 dayCount 为 2 时,返回的数组为 ['2023-01-02', '2023-01-06']。 **参数**: - `date` (Date | number | string, 可选): 指定日期,默认为当前日期。 - `dayCount` (number, 可选): 目标天数偏移量,默认为 0。 **返回值**: - `[string, string]`: 返回一个包含第一天和目标天的数组,格式为 'YYYY-MM-DD'。 **使用示例**: ```typescript const [firstDay, targetDay] = getFirstAndLastDayOfWeekWithoutNext('2023-01-04', 2); console.log(firstDay, targetDay); // '2023-01-02', '2023-01-06' ``` ### getFirstAndLastDayOfMonth 获取指定日期所在月的第一天和最后一天。 **说明**: 该方法用于获取指定日期所在月的第一天和最后一天的日期。例如,当传入的日期为 '2023-01-15' 时,返回的数组为 ['2023-01-01', '2023-01-31']。 **参数**: - `date` (Date | number | string, 可选): 指定日期,默认为当前日期。 **返回值**: - `[string, string]`: 返回一个包含第一天和最后一天的数组,格式为 'YYYY-MM-DD'。 **使用示例**: ```typescript const [firstDay, lastDay] = getFirstAndLastDayOfMonth('2023-01-15'); console.log(firstDay, lastDay); // '2023-01-01', '2023-01-31' ``` ### getYearsInRange 获取指定年份范围内的所有年份。 **说明**: 该方法用于获取指定年份范围内的所有年份。例如,当传入的起始年份为 '2020' 和结束年份为 '2023' 时,返回的数组为 ['2020', '2021', '2022', '2023']。 **参数**: - `startYear` (number | string | Date): 起始年份。 - `endYear` (number | string | Date): 结束年份。 **返回值**: - `string[]`: 返回包含所有年份的数组。 **使用示例**: ```typescript const years = getYearsInRange('2020', '2023'); console.log(years); // ['2020', '2021', '2022', '2023'] ``` ### getDatesInRange 获取指定日期范围内的所有日期。 **说明**: 该方法用于获取指定日期范围内的所有日期。例如,当传入的起始日期为 '2023-01-01' 和结束日期为 '2023-01-05' 时,返回的数组为 ['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04', '2023-01-05']。 **参数**: - `startDate` (string | Date | number): 起始日期。 - `endDate` (string | Date | number): 结束日期。 **返回值**: - `string[]`: 返回包含所有日期的数组,格式为 'YYYY-MM-DD'。 **使用示例**: ```typescript const dates = getDatesInRange('2023-01-01', '2023-01-05'); console.log(dates); // ['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04', '2023-01-05'] ``` ### getMonthsInRange 获取指定日期范围内的所有月份。 **说明**: 该方法用于获取指定日期范围内的所有月份。例如,当传入的起始日期为 '2023-01-01' 和结束日期为 '2023-03-01' 时,返回的数组为 ['2023-01', '2023-02', '2023-03']。 **参数**: - `startDate` (Date | string | number): 起始日期。 - `endDate` (Date | string | number): 结束日期。 **返回值**: - `string[]`: 返回包含所有月份的数组,格式为 'YYYY-MM'。 **使用示例**: ```typescript const months = getMonthsInRange('2023-01-01', '2023-03-01'); console.log(months); // ['2023-01', '2023-02', '2023-03'] ``` ### getMonthsByDates 获取指定日期数组中的所有月份。 **说明**: 该方法用于获取指定日期数组中的所有月份。例如,当传入的日期数组为 ['2023-01-01', '2023-02-15', '2023-03-10'] 时,返回的数组为 ['2023-01', '2023-02', '2023-03']。 **参数**: - `dates` (string[]): 日期数组。 **返回值**: - `string[]`: 返回包含所有月份的数组,格式为 'YYYY-MM'。 **使用示例**: ```typescript const months = getMonthsByDates(['2023-01-01', '2023-02-15', '2023-03-10']); console.log(months); // ['2023-01', '2023-02', '2023-03'] ``` ### getDatesInMonths 获取指定月份数组中的所有日期。 **说明**: 该方法用于获取指定月份数组中的所有日期。例如,当传入的月份数组为 ['2023-01', '2023-02'] 时,返回的数组为 ['2023-01-01', '2023-01-02', ..., '2023-02-28']。 **参数**: - `months` (string[]): 月份数组。 **返回值**: - `string[]`: 返回包含所有日期的数组,格式为 'YYYY-MM-DD'。 **使用示例**: ```typescript const dates = getDatesInMonths(['2023-01', '2023-02']); console.log(dates); // ['2023-01-01', '2023-01-02', ..., '2023-02-28'] ``` ### getMonthsInYears 获取指定年份数组中的所有月份。 **说明**: 该方法用于获取指定年份数组中的所有月份。例如,当传入的年份数组为 ['2022', '2023'] 时,返回的数组为 ['2022-01', '2022-02', ..., '2023-12']。 **参数**: - `years` (string[]): 年份数组。 **返回值**: - `string[]`: 返回包含所有月份的数组,格式为 'YYYY-MM'。 **使用示例**: ```typescript const months = getMonthsInYears(['2022', '2023']); console.log(months); // ['2022-01', '2022-02', ..., '2023-12'] ``` ### getMonthsInYearsWithoutNext 获取指定年份数组中的所有月份(不包括未来的月份)。 **说明**: 该方法用于获取指定年份数组中的所有月份,但不包括未来的月份。天数偏移量用于指定相对于当前日期的偏移量。例如,当传入的年份数组为 ['2022', '2023'] 且当前日期为 '2023-01-15' 时,返回的数组为 ['2022-01', '2022-02', ..., '2023-01']。 **参数**: - `years` (string[]): 年份数组。 - `daysOffset` (number, 可选): 天数偏移量,默认为 0。 **返回值**: - `string[]`: 返回包含所有月份的数组,格式为 'YYYY-MM'。 **使用示例**: ```typescript const months = getMonthsInYearsWithoutNext(['2022', '2023']); console.log(months); // ['2022-01', '2022-02', ..., '2023-01'] ``` ### getDatesInMonthsWithoutNext 获取指定月份数组中的所有日期(不包括未来的日期)。 **说明**: 该方法用于获取指定月份数组中的所有日期,但不包括未来的日期。天数偏移量用于指定相对于当前日期的偏移量。例如,当传入的月份数组为 ['2023-01'] 且当前日期为 '2023-01-15' 时,返回的数组为 ['2023-01-01', '2023-01-02', ..., '2023-01-15']。 **参数**: - `months` (string[]): 月份数组。 - `dayCount` (number, 可选): 天数偏移量,默认为 0。 **返回值**: - `string[]`: 返回包含所有日期的数组,格式为 'YYYY-MM-DD'。 **使用示例**: ```typescript const dates = getDatesInMonthsWithoutNext(['2023-01']); console.log(dates); // ['2023-01-01', '2023-01-02', ..., '2023-01-15'] ``` ### convertSecondsToTime 将秒数转换为时间字符串。 **说明**: 该方法用于将秒数转换为时间字符串,格式为 'X天X小时X分钟X秒'。例如,当传入的秒数为 90061 时,返回的字符串为 '1天1小时1分钟1秒'。 **参数**: - `time` (number): 秒数。 **返回值**: - `string`: 返回转换后的时间字符串。 **使用示例**: ```typescript const timeString = convertSecondsToTime(90061); console.log(timeString); // '1天1小时1分钟1秒' ```