head

源码

https://github.com/ddzy/ts-utility-plugins/tree/master/src/ddzy/utility/array/head

说明

获取指定数组的第个元素

用法

head.ts
head.test.ts
import { head } from "./ddzy/utility/array/head";
// ? 普通数字数组
const s1: number[] = [1, 2, 3, 4, 5];
const p1 = head<number>(s1);
console.log(p1);
console.log('-----------------');
// ? 空数组
const s2: any[] = [];
const p2 = head<any>(s2);
console.log(p2);
import { head } from "../../utility/array/head";
describe('head tests: ', () => {
test('head should return the first field that was in origin array', () => {
const received: number[] = [1, 2, 3, 4, 5];
const expected = 1;
const result = head<number>(received);
expect(result).toBe(expected);
});
test('head should return undefined when receive an empty array', () => {
const received: any[] = [];
const expected = undefined;
const result = head<any>(received);
expect(result).toBe(expected);
});
});