size

源码

https://github.com/ddzy/ts-utility-plugins/tree/master/src/ddzy/utility/others/size

说明

返回collection(集合)的长度, 如果集合是类数组字符串, 返回其 length;

如果集合是对象, 返回其可枚举属性的个数.

用法

size.ts
size.test.ts
size.ts
import { size } from "./ddzy/utility/others/sizse";
// ? 特殊值
const s0 = [0, 100, undefined, null, Symbol('ddzy'), function () { }];
const p0 = s0.map((v) => {
return size(v);
});
console.log(p0); // [0, 0, 0, 0, 0, 0]
console.log('------------------------');
// ? 字符串
const s1 = ['', 'ddzy'];
const p1 = s1.map((v) => {
return size(v);
});
console.log(p1); // [0, 4]
console.log('-----------------------');
// ? 数组
const s2 = [[], [0, 1, 2, 3, 4, 5]];
const p2 = s2.map((v) => {
return size(v);
});
console.log(p2); // [0, 6]
console.log('------------------------');
// ? 普通对象
const s3 = [{}, { name: 'duanzhaoyang', age: 21 }];
const p3 = s3.map((v) => {
return size(v);
});
console.log(p3); // [0, 2]
size.test.ts
import { size } from "../../utility/others/sizse";
describe('size tests...', () => {
test('size() should return zero when receive a special character', () => {
const received = [0, 100, undefined, null, Symbol('ddzy'), function () { }];
const expected = [0, 0, 0, 0, 0, 0];
received.forEach((v, i) => {
const result = size(v);
expect(result).toBe(expected[i]);
});
});
test('size() should return the correct length of string', () => {
const received = ['', 'ddzy'];
const expected = [0, 4];
received.forEach((v, i) => {
const result = size(v);
expect(result).toBe(expected[i]);
});
});
test('size() should return the correct length of array', () => {
const received = [[], [0, 1, 2, 3, 4, 5]];
const expected = [0, 6];
received.forEach((v, i) => {
const result = size(v);
expect(result).toBe(expected[i]);
});
});
test('size() should return the correct length of array', () => {
const received = [{}, { name: 'duanzhaoyang', age: 21 }];
const expected = [0, 2];
received.forEach((v, i) => {
const result = size(v);
expect(result).toBe(expected[i]);
});
});
});