-
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.
Merge branch 'dev' of https://github.com/chenbimo/yidash into dev
- Loading branch information
Showing
18 changed files
with
289 additions
and
34 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,26 @@ | ||
/** | ||
* 随机取数组中的元素 | ||
* @author XiaoXinYo | ||
* @category 数组 | ||
* @param {Array} array 数组 | ||
* @param {number} number 数量 | ||
* @param {Boolean} repeat 是否允许重复 | ||
* @returns {Array} 返回取出元素的数组 | ||
*/ | ||
|
||
export default (array, number = 1, repeat = false) => { | ||
const temp_array = [...new Set(array)]; | ||
if (!repeat && number > temp_array.length) { | ||
throw new Error('所需数量超出数组的不重复元素数量'); | ||
} | ||
|
||
const result = []; | ||
for (let i = 0; i < number; i++) { | ||
let itemIndex = Math.floor(Math.random() * temp_array.length); | ||
result.push(temp_array[itemIndex]); | ||
if (!repeat) { | ||
temp_array.splice(itemIndex, 1); | ||
} | ||
} | ||
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,55 @@ | ||
/** | ||
* @name irregularSorting | ||
* @author [email protected] | ||
* @description 无规则图片排序 | ||
* @param { * Array} picList 图片列表 | ||
* @param { * dom } 在dom 上展示 | ||
* @param { * num } 每行展示个数 | ||
* @category 浏览器 | ||
*/ | ||
|
||
function irregularSorting(picList, containerDom, num) { | ||
if (!containerDom || !picList.length) return; | ||
let screenWidth = containerDom.clientWidth; | ||
picList.forEach(async (item, index) => { | ||
const img = document.createElement("img"); | ||
img.src = item.url; | ||
img.style.width = screenWidth / (num + 0.8) + "px"; | ||
img.style.position = "absolute"; | ||
img.loading = "lazy"; | ||
img.style.left = !(index % num) | ||
? "10px" | ||
: (screenWidth / num) * (index % num) + 10 + "px"; | ||
|
||
if (index < num) { | ||
img.style.top = containerDom.offsetTop + "px"; | ||
} else { | ||
let topAndLeft = []; | ||
for (let i = 0; i < containerDom.childNodes.length; i++) { | ||
topAndLeft.push({ | ||
left: containerDom.childNodes[i].offsetLeft, | ||
top: | ||
containerDom.childNodes[i].offsetTop + | ||
containerDom.childNodes[i].offsetHeight, | ||
}); | ||
} | ||
const allListSort = topAndLeft.sort((a, b) => a.top - b.top); | ||
const needList = allListSort.slice( | ||
allListSort.length - num, | ||
allListSort.length | ||
); | ||
const finallyTop = needList.sort((a, b) => a.top - b.top)[0]; | ||
img.style.top = finallyTop.top + 10 + "px"; | ||
img.style.left = finallyTop.left + "px"; | ||
} | ||
await containerDom.appendChild(img); | ||
}); | ||
let heightList = []; | ||
for (let i = 0; i < containerDom.children.length; i++) { | ||
heightList.push( | ||
containerDom.childNodes[i].offsetTop + | ||
containerDom.childNodes[i].offsetHeight | ||
); | ||
} | ||
containerDom.style.height = heightList.sort((a, b) => b - a)[0] + "px"; | ||
} |
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,22 @@ | ||
/** | ||
* 常见的姓氏 | ||
*/ | ||
const surnames = ["赵", "钱", "孙", "李", "周", "吴", "郑", "王"]; | ||
|
||
/** | ||
* 常见的名字 | ||
*/ | ||
const names = ["子璇", "淼", "国栋", "夫子", "瑞堂", "甜", "敏", "尚", "国贤", "贺祥", "晨涛", "昊轩", "易轩", "辰益", "帆", "冉", "瑾", "春", "瑾昆", "春齐", "杨", "文昊", "东雄", "霖", "浩晨", "熙涵", "溶溶", "冰枫", "欣宜", "豪", "欣慧", "建政", "美欣", "淑慧", "文轩", "杰", "欣源", "忠林", "榕润", "欣汝", "慧嘉", "新建", "建林", "亦菲", "林", "冰洁", "佳欣", "涵涵", "禹辰", "淳美", "泽惠", "伟洋", "涵越", "润丽", "翔", "淑华", "晶莹", "凌晶", "苒溪", "雨涵", "嘉怡", "佳毅", "子辰", "佳琪", "紫轩", "瑞辰", "昕蕊", "萌", "明远", "欣宜", "泽远", "欣怡", "佳怡", "佳惠", "晨茜", "晨璐", "运昊", "汝鑫", "淑君", "晶滢", "润莎", "榕汕", "佳钰", "佳玉", "晓庆", "一鸣", "语晨", "添池", "添昊", "雨泽", "雅晗", "雅涵", "清妍", "诗悦", "嘉乐", "晨涵", "天赫", "玥傲", "佳昊", "天昊", "萌萌", "若萌"]; | ||
/** | ||
* 生成一个姓名 | ||
* @author xiaoxiaohuayu <https://github.com/xiaoxiaohuayu> | ||
* @example | ||
* console.log(generateChineseName()); // 输出: 王国栋 | ||
* @returns {String} 返回一个随机的中文姓名 | ||
* @summary 应用场景:用于生成测试数据 | ||
*/ | ||
export function generateChineseName () { | ||
const surname = surnames[Math.floor(Math.random() * surnames.length)]; | ||
const name = names[Math.floor(Math.random() * names.length)]; | ||
return surname + name | ||
} |
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,26 @@ | ||
/** | ||
* 生成一个车牌号 | ||
* @author xiaoxiaohuayu <https://github.com/xiaoxiaohuayu> | ||
* @example | ||
* console.log(generateLicensePlateNumber()); // 输出: 闽ASXRG4 | ||
* @summary 应用场景:用于生成测试数据 | ||
* @param {Number} total 生成车牌号的位数 (新能源车牌号为6位,普通车牌号为5位) 默认5位 | ||
* @returns {String} 返回一个随机的车牌号 | ||
*/ | ||
const dicingChar = (series) => { | ||
return series[~~(Math.random() * series.length)] | ||
} | ||
export function generateLicensePlateNumber (total = 5) { | ||
const stateList = '京津冀晋辽吉沪苏浙皖闽琼赣鲁豫鄂湘粤渝川贵云陕甘蒙黑桂藏青宁新' | ||
const charList = 'ABCDEFGHJKLMNQPRSTUVWXYZ' | ||
const numList = '1234567890' | ||
const halfList = [charList, numList] | ||
const state = dicingChar(stateList) | ||
const city = dicingChar(charList) | ||
let sequence = '' | ||
while (total--) { | ||
sequence += dicingChar(halfList[Math.round(Math.random())]) | ||
} | ||
// console.log(`${state}${city}${sequence}`) | ||
return `${state}${city}${sequence}` | ||
} |
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,23 @@ | ||
/** | ||
* 生成一个手机号 | ||
* @author xiaoxiaohuayu <https://github.com/xiaoxiaohuayu> | ||
* @example | ||
* console.log(generateMobleTel()); // 输出: 15105113629 | ||
* @returns {String} 返回一个随机的手机号 | ||
* @summary 应用场景:用于生成测试数据 | ||
*/ | ||
export function generateMobleTel () { | ||
// China Mobile 移动号段 | ||
const c_Mobile = ['134', '147', '159', '184', '135', '150', '165', '187', '136', '151', '172', '188', '137', '152', '178', '195', '138', '157', '182', '197', '139', '158', '183', '198']; | ||
// China Unicom 联通号段 | ||
const c_Unicom = ['130', '156', '185', '131', '166', '186', '132', '167', '196', '145', '171', '146', '175', '155', '176'] | ||
// China Telecom 电信号段 | ||
const c_Telecom = ['133', '177', '199', '149', '180', '153', '181', '162', '189', '173', '191', '174', '193'] | ||
const prefixes = c_Mobile.concat(c_Unicom, c_Telecom) | ||
const prefix = prefixes[Math.floor(prefixes.length * Math.random())]; | ||
let body = ""; | ||
for (let i = 0; i < 8; i++) { | ||
body += Math.floor(Math.random() * 10); | ||
} | ||
return prefix + body; | ||
} |
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import isObject from '../../lib/is/object'; | ||
import isFunction from '../../lib/is/function'; | ||
|
||
/** | ||
* 对象属性摘取 | ||
* @author imddc <https://github.com/imddc> | ||
* @category 对象 | ||
* @param {Object} obj 对象数据 | ||
* @param {Funcion} fn 获取字段的方式 | ||
* @returns {Object} 摘取对象中的指定字段 | ||
*/ | ||
export default (obj, fn) => { | ||
if (!isObject(obj)) { | ||
throw new Error('obj must be an object'); | ||
} | ||
|
||
if (!isFunction(fn)) { | ||
throw new Error('fn must be an function'); | ||
} | ||
|
||
if (Object.keys(obj).length === 0) { | ||
return obj; | ||
} | ||
|
||
const picked = {}; | ||
for (const key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
if (fn(obj[key])) { | ||
picked[key] = obj[key]; | ||
} | ||
} | ||
} | ||
return picked; | ||
}; |
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,39 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import yd_array_randomItem from '../../lib/array/randomItem.js'; | ||
|
||
describe('yd_array_randomItem', () => { | ||
it('returns specified number of items', () => { | ||
const array = [1, 2, 3, 4, 5]; | ||
const result = [...new Set(yd_array_randomItem(array, 3))]; | ||
result.forEach((item) => { | ||
expect(array).toContain(item); | ||
}); | ||
expect(result).toHaveLength(3); | ||
}); | ||
|
||
it('returns repeating items when repeat is true', () => { | ||
const array = [1, 2, 3, 4, 5]; | ||
const result = yd_array_randomItem(array, 3, true); | ||
result.forEach((item) => { | ||
expect(array).toContain(item); | ||
}); | ||
expect(result).toHaveLength(3); | ||
}); | ||
|
||
it('when the required quantity exceeds the number of non repeating elements in the array and non repeating is required, an exception will be raised', () => { | ||
const array = [1, 2, 3, 3]; | ||
const testFunc = () => { | ||
yd_array_randomItem(array, 4, false); | ||
}; | ||
expect(testFunc).toThrow('所需数量超出数组的不重复元素数量'); | ||
}); | ||
|
||
it('when repeat is true and the required number exceeds the number of non repeating elements in the array, return a duplicate item', () => { | ||
const array = [1, 2, 3, 3]; | ||
const result = yd_array_randomItem(array, 4, true); | ||
result.forEach((item) => { | ||
expect(array).toContain(item); | ||
}); | ||
expect(result).toHaveLength(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import yd_is_odd from '../../lib/is/odd'; | ||
import { describe, expect, it } from 'vitest' | ||
import yd_is_odd from '../../lib/is/odd' | ||
|
||
describe('yd_is_odd', () => { | ||
it('should be `true`', () => { | ||
expect(yd_is_odd(13)).toBeTruthy(); | ||
expect(yd_is_odd(-13)).toBeTruthy(); | ||
}); | ||
expect(yd_is_odd(13)).toBeTruthy() | ||
expect(yd_is_odd(-13)).toBeTruthy() | ||
}) | ||
|
||
it('should be `false`', () => { | ||
expect(isOdd(0)).toBeFalsy(); | ||
expect(isOdd(2)).toBeFalsy(); | ||
expect(isOdd(-2)).toBeFalsy(); | ||
}); | ||
expect(yd_is_odd(0)).toBeFalsy() | ||
expect(yd_is_odd(2)).toBeFalsy() | ||
expect(yd_is_odd(-2)).toBeFalsy() | ||
}) | ||
|
||
it('should throw Error', () => { | ||
expect(() => isOdd(true)).toThrowError('value must be a number'); | ||
expect(() => isOdd(Symbol())).toThrowError('value must be a number'); | ||
expect(() => isOdd([])).toThrowError('value must be a number'); | ||
expect(() => isOdd({})).toThrowError('value must be a number'); | ||
expect(() => isOdd(new Date())).toThrowError('value must be a number'); | ||
expect(() => yd_is_odd(true)).toThrowError('value must be a number') | ||
expect(() => yd_is_odd(Symbol(''))).toThrowError('value must be a number') | ||
expect(() => yd_is_odd([])).toThrowError('value must be a number') | ||
expect(() => yd_is_odd({})).toThrowError('value must be a number') | ||
expect(() => yd_is_odd(new Date())).toThrowError('value must be a number') | ||
// ... | ||
}); | ||
}); | ||
}) | ||
}) |
Oops, something went wrong.