Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/chenbimo/yidash into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
chenbimo committed Aug 1, 2024
2 parents abf486f + 0bdb006 commit 316c623
Show file tree
Hide file tree
Showing 18 changed files with 289 additions and 34 deletions.
26 changes: 26 additions & 0 deletions lib/array/randomItem.js
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;
};
55 changes: 55 additions & 0 deletions lib/browser/irregularSorting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @name irregularSorting
* @author wjr13294670259@163.com
* @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";
}
22 changes: 22 additions & 0 deletions lib/faker/generateChineseName.js
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
}
26 changes: 26 additions & 0 deletions lib/faker/generateLicensePlateNumber.js
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}`
}
23 changes: 23 additions & 0 deletions lib/faker/generateMobleTel.js
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;
}
4 changes: 2 additions & 2 deletions lib/is/boolean.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { yd_internal_getValueType } from '../internal/getValueType.js';
import getValueType from '../internal/getValueType.js';
/**
* 判断是否是布尔值
* @author 陈随易 <https://chensuiyi.me>
Expand All @@ -7,5 +7,5 @@ import { yd_internal_getValueType } from '../internal/getValueType.js';
* @returns {Boolean} 返回是否是布尔值
*/
export default (value) => {
return value === true || value === false || yd_internal_getValueType(value) === '[object Boolean]';
return value === true || value === false || getValueType(value) === '[object Boolean]';
};
4 changes: 2 additions & 2 deletions lib/is/date.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { yd_internal_getValueType } from '../internal/getValueType.js';
import getValueType from '../internal/getValueType.js';
/**
* 判断是否是日期
* @author 陈随易 <https://chensuiyi.me>
Expand All @@ -7,5 +7,5 @@ import { yd_internal_getValueType } from '../internal/getValueType.js';
* @returns {Boolean} 返回是否是日期
*/
export default (value) => {
return yd_internal_getValueType(value) === '[object Date]';
return getValueType(value) === '[object Date]';
};
4 changes: 2 additions & 2 deletions lib/is/function.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { yd_internal_getValueType } from '../internal/getValueType.js';
import getValueType from '../internal/getValueType.js';
/**
* 判断是否是函数
* @author 陈随易 <https://chensuiyi.me>
Expand All @@ -7,6 +7,6 @@ import { yd_internal_getValueType } from '../internal/getValueType.js';
* @returns {Boolean} 返回是否是函数
*/
export default (value) => {
const tag = yd_internal_getValueType(value);
const tag = getValueType(value);
return tag === '[object Function]' || tag === '[object AsyncFunction]';
};
4 changes: 2 additions & 2 deletions lib/is/map.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { yd_internal_getValueType } from '../internal/getValueType.js';
import getValueType from '../internal/getValueType.js';
/**
* 判断是否是Map值
* @author 陈随易 <https://chensuiyi.me>
Expand All @@ -7,5 +7,5 @@ import { yd_internal_getValueType } from '../internal/getValueType.js';
* @returns {Boolean} 返回是否是Map值
*/
export default (value) => {
return yd_internal_getValueType(value) === '[object Map]';
return getValueType(value) === '[object Map]';
};
4 changes: 2 additions & 2 deletions lib/is/regexp.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { yd_internal_getValueType } from '../internal/getValueType.js';
import getValueType from '../internal/getValueType.js';
/**
* 判断是否是正则表达式
* @author 陈随易 <https://chensuiyi.me>
Expand All @@ -7,5 +7,5 @@ import { yd_internal_getValueType } from '../internal/getValueType.js';
* @returns {Boolean} 返回是否是正则表达式
*/
export default (value) => {
return yd_internal_getValueType(value) === '[object RegExp]';
return getValueType(value) === '[object RegExp]';
};
4 changes: 2 additions & 2 deletions lib/is/set.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { yd_internal_getValueType } from '../internal/getValueType.js';
import getValueType from '../internal/getValueType.js';
/**
* 判断是否是Set值
* @author 陈随易 <https://chensuiyi.me>
Expand All @@ -7,5 +7,5 @@ import { yd_internal_getValueType } from '../internal/getValueType.js';
* @returns {Boolean} 返回是否是Set值
*/
export default (value) => {
return yd_internal_getValueType(value) === '[object Set]';
return getValueType(value) === '[object Set]';
};
4 changes: 2 additions & 2 deletions lib/is/symbol.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { yd_internal_getValueType } from '../internal/getValueType.js';
import getValueType from '../internal/getValueType.js';
/**
* 判断是否是Symbol值
* @author 陈随易 <https://chensuiyi.me>
Expand All @@ -7,5 +7,5 @@ import { yd_internal_getValueType } from '../internal/getValueType.js';
* @returns {Boolean} 返回是否是Symbol值
*/
export default (value) => {
return yd_internal_getValueType(value) === '[object Symbol]';
return getValueType(value) === '[object Symbol]';
};
4 changes: 2 additions & 2 deletions lib/is/weakMap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { yd_internal_getValueType } from '../internal/getValueType.js';
import getValueType from '../internal/getValueType.js';
/**
* 判断是否是WeakMap值
* @author 陈随易 <https://chensuiyi.me>
Expand All @@ -7,5 +7,5 @@ import { yd_internal_getValueType } from '../internal/getValueType.js';
* @returns {Boolean} 返回是否是WeakMap值
*/
export default (value) => {
return yd_internal_getValueType(value) === '[object WeakMap]';
return getValueType(value) === '[object WeakMap]';
};
4 changes: 2 additions & 2 deletions lib/is/weakSet.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { yd_internal_getValueType } from '../internal/getValueType.js';
import getValueType from '../internal/getValueType.js';
/**
* 判断是否是WeakSet值
* @author 陈随易 <https://chensuiyi.me>
Expand All @@ -7,5 +7,5 @@ import { yd_internal_getValueType } from '../internal/getValueType.js';
* @returns {Boolean} 返回是否是WeakSet值
*/
export default (value) => {
return yd_internal_getValueType(value) === '[object WeakSet]';
return getValueType(value) === '[object WeakSet]';
};
34 changes: 34 additions & 0 deletions lib/object/pickBy.js
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;
};
39 changes: 39 additions & 0 deletions test/array/randomItem.test.js
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);
});
});
32 changes: 16 additions & 16 deletions test/is/odd.test.js
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')
// ...
});
});
})
})
Loading

0 comments on commit 316c623

Please sign in to comment.