-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
91 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* 数组按条件过滤 | ||
* @alias yd_array_filterBy | ||
* @category array | ||
* @param {Array} arrs 数组数据 | ||
* @param {Function} fn 比对函数 | ||
* @returns {object} 返回根据字段映射的对象 | ||
* @author 陈随易 <https://chensuiyi.me> | ||
* @example yd_array_filterBy() | ||
*/ | ||
export default (arrs, fn) => { | ||
const result = arrs.filter((item) => { | ||
return fn(item); | ||
}); | ||
return result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { it, expect, describe } from 'vitest'; | ||
import yd_array_filterBy from './filterBy.js'; | ||
|
||
describe('yd_array_filterBy', () => { | ||
it('应该返回 true', () => { | ||
const arrs = [ | ||
{ type: 'food', value: 1 }, | ||
{ type: 'book', value: 2 }, | ||
{ type: 'book', value: 3 }, | ||
{ type: 'food', value: 4 }, | ||
{ type: 'book', value: 5 } | ||
]; | ||
const result = yd_array_filterBy(arrs, (item) => item.type === 'food'); | ||
expect(result).toStrictEqual([ | ||
{ type: 'food', value: 1 }, | ||
{ type: 'food', value: 4 } | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,52 @@ | ||
import { format, formatDistanceToNow } from 'date-fns'; | ||
import { zhCN } from 'date-fns/locale'; | ||
// 转换相对时间 | ||
const _convertTime = (obj) => { | ||
try { | ||
const item = {}; | ||
for (let key in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
const value = obj[key]; | ||
if (key.endsWith('_at')) { | ||
let key1 = key.replace('_at', '_at1'); | ||
let key2 = key.replace('_at', '_at2'); | ||
let dt = new Date(value); | ||
if (value !== 0) { | ||
item[key] = value; | ||
item[key1] = format(dt, 'yyyy-MM-dd HH:mm:ss'); | ||
item[key2] = formatDistanceToNow(dt, { locale: zhCN, addSuffix: true }); | ||
} else { | ||
item[key] = ''; | ||
} | ||
} else { | ||
item[key] = value; | ||
} | ||
} | ||
} | ||
|
||
return item; | ||
} catch (err) { | ||
console.log('🚀 ~ err:', err); | ||
} | ||
}; | ||
|
||
/** | ||
* 转换相对时间 | ||
* @alias yd_datetime_relativeTime | ||
* @category datetime | ||
* @param {Array | object} data 数组或对象 | ||
* @returns {object} 返回转换后的相对时间 | ||
* @author 陈随易 <https://chensuiyi.me> | ||
* @example | ||
*/ | ||
export default (data) => { | ||
// 如果是数组 | ||
if (Array.isArray(data)) { | ||
return data.map((item) => { | ||
return _convertTime(item); | ||
}); | ||
} | ||
|
||
// 如果是对象 | ||
return _convertTime(data); | ||
}; | ||
import { format, formatDistanceToNow } from 'date-fns'; | ||
import { zhCN } from 'date-fns/locale'; | ||
// 转换相对时间 | ||
const _convertTime = (obj) => { | ||
try { | ||
const item = {}; | ||
for (let key in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
const value = obj[key]; | ||
if (key.endsWith('_at')) { | ||
let key1 = key.replace('_at', '_at1'); | ||
let key2 = key.replace('_at', '_at2'); | ||
let dt = new Date(value); | ||
if (value !== 0) { | ||
item[key] = value; | ||
item[key1] = format(dt, 'yyyy-MM-dd HH:mm:ss'); | ||
item[key2] = formatDistanceToNow(dt, { locale: zhCN, addSuffix: true }); | ||
} else { | ||
item[key] = ''; | ||
} | ||
} else { | ||
item[key] = value; | ||
} | ||
} | ||
} | ||
|
||
return item; | ||
} catch (err) { | ||
console.log('🚀 ~ err:', err); | ||
} | ||
}; | ||
|
||
/** | ||
* 转换相对时间 | ||
* @alias yd_datetime_relativeTime | ||
* @category datetime | ||
* @param {Array | object} data 数组或对象 | ||
* @returns {object} 返回转换后的相对时间 | ||
* @author 陈随易 <https://chensuiyi.me> | ||
* @example yd_datetime_relativeTime([]) | ||
*/ | ||
export default (data) => { | ||
// 如果是数组 | ||
if (Array.isArray(data)) { | ||
return data.map((item) => { | ||
return _convertTime(item); | ||
}); | ||
} | ||
|
||
// 如果是对象 | ||
return _convertTime(data); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters