Skip to content

Commit

Permalink
Merge pull request #40 from behzadam/39-refactor-generic-names
Browse files Browse the repository at this point in the history
39 refactor generic names
  • Loading branch information
behzadam committed Jun 26, 2023
2 parents 8136afd + 6bfffb9 commit 829126b
Show file tree
Hide file tree
Showing 22 changed files with 59 additions and 61 deletions.
20 changes: 6 additions & 14 deletions src/are-same.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import { areSame } from './';
import { areSame } from './are-same';

describe('areSame valid cases', () => {
test.each`
input | expected
${[1, 1, 1]} | ${true}
${['one', 'one', 'one']} | ${true}
${[1.5, 1.5, 1.5]} | ${true}
${[true, true, true]} | ${true}
`('should return $expected when input is: $input', ({ input, expected }) => {
expect(areSame(input)).toBe(expected);
});
});

describe('areEqual invalid cases', () => {
describe('areSame', () => {
test.each`
input | expected
${[1, 1, 1]} | ${true}
${['one', 'one', 'one']} | ${true}
${[1.5, 1.5, 1.5]} | ${true}
${[true, true, true]} | ${true}
${[1, 2, 3]} | ${false}
${['one', 'two', 'three']} | ${false}
${[1.5, 2.5, 3.5]} | ${false}
Expand Down
6 changes: 4 additions & 2 deletions src/are-same.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Primitive } from './types';

/**
* Compares all items in the given array and returns true if they are same.
* @param array - Given array.
Expand All @@ -11,8 +13,8 @@
* ```
* @alpha
*/
export function areSame<T extends string | number | boolean>(
array: Required<T[]>,
export function areSame<Item extends Primitive>(
array: Required<Item[]>,
): boolean {
return array.every((item) => item === array[0]);
}
2 changes: 1 addition & 1 deletion src/count-items.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { countItems } from './';
import { countItems } from './count-items';

describe('countItems cases', () => {
test.each`
Expand Down
6 changes: 4 additions & 2 deletions src/count-items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import { Primitive } from './types';
* ```
* @beta
*/
export function countItems<T extends Primitive>(array: T[]): Record<string, T> {
export function countItems<Item extends Primitive>(
array: Item[],
): Record<string, Item> {
return array.reduce((acc: any, current) => {
acc[current] = !acc[current] ? 1 : ++acc[current];
return acc;
}, {} as Record<string, T>);
}, {} as Record<string, Item>);
}
2 changes: 1 addition & 1 deletion src/get-length.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getLength } from './';
import { getLength } from './get-length';

describe('getLength cases', () => {
test.each`
Expand Down
12 changes: 6 additions & 6 deletions src/get-length.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import { isString } from './is-string';

/**
* Gets length of string | array | object
* @param value - string | array | object
* @param input - string | array | object
* @returns length of the input or 0
*
* @public
*/
export function getLength<T>(value: T): number {
if (isString(value) || isArray(value)) {
return value.length;
export function getLength<Input>(input: Input): number {
if (isString(input) || isArray(input)) {
return input.length;
}

if (isObject(value)) {
return Object.keys(value).length;
if (isObject(input)) {
return Object.keys(input).length;
}
// For primitive types:
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/has-duplicates.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hasDuplicates } from './';
import { hasDuplicates } from './has-duplicates';

describe('hasDuplicates cases', () => {
test.each`
Expand Down
4 changes: 3 additions & 1 deletion src/has-duplicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { Primitive } from './types';
*
* @public
*/
export function hasDuplicates<T extends keyof Primitive>(array: T[]): boolean {
export function hasDuplicates<Item extends keyof Primitive>(
array: Item[],
): boolean {
return new Set(array).size < array.length;
}
2 changes: 1 addition & 1 deletion src/index-by.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { indexBy } from '.';
import { indexBy } from './index-by';

describe('indexBy cases', () => {
const withUniqueKey = [
Expand Down
8 changes: 4 additions & 4 deletions src/index-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
* @public
*/
export function indexBy<
T extends { [K in keyof T]: string | number | symbol },
K extends keyof T,
>(array: T[], key: K): Record<T[K], T> {
Item extends { [Key in keyof Item]: string | number | symbol },
Key extends keyof Item,
>(array: Item[], key: Key): Record<Item[Key], Item> {
return array.reduce(
(acc, item) => ({ ...acc, [item[key]]: item }),
{} as Record<T[K], T>,
{} as Record<Item[Key], Item>,
);
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export { hasDuplicates } from './has-duplicates';
export { isArray } from './is-array';
export { isArrayOfString } from './is-array-of-string';
export { isDate } from './is-date';
export { isDateString as isDateValid } from './is-date-string';
export { isStringDate as isDateValid } from './is-string-date';
export { isDefined } from './is-defined';
export { isEmpty } from './is-empty';
export { isEqual } from './is-equal';
Expand Down
2 changes: 1 addition & 1 deletion src/is-array-of-string.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isArrayOfString } from './';
import { isArrayOfString } from './is-array-of-string';

describe('isArrayOfString cases', () => {
test.each`
Expand Down
6 changes: 3 additions & 3 deletions src/is-array-of-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isString } from './is-string';

/**
* Determines whether the input is array of string or not.
* @param value - input
* @param input - input
* @returns true if the input is array, false otherwise.
* @example
* ```ts
Expand All @@ -11,6 +11,6 @@ import { isString } from './is-string';
* ```
* @public
*/
export function isArrayOfString(value: unknown): value is string[] {
return Array.isArray(value) && value.every((item) => isString(item));
export function isArrayOfString(input: unknown): input is string[] {
return Array.isArray(input) && input.every((item) => isString(item));
}
2 changes: 1 addition & 1 deletion src/is-array.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isArray } from './';
import { isArray } from './is-array';

describe('isArray cases', () => {
test.each`
Expand Down
6 changes: 3 additions & 3 deletions src/is-array.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Determines whether the input is an array or not.
* @param value - Generic input.
* @param input - Generic input.
* @returns true if the input is an array or false otherwise.
* @example
* ```ts
Expand All @@ -11,6 +11,6 @@
* ```
* @public
*/
export function isArray<T>(value: unknown): value is Array<T> {
return Array.isArray(value);
export function isArray<Input>(input: unknown): input is Array<Input> {
return Array.isArray(input);
}
4 changes: 2 additions & 2 deletions src/is-date-string.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isDateValid } from './';
import { isStringDate } from './';

describe('isDateValid valid cases', () => {
test.each`
Expand All @@ -17,6 +17,6 @@ describe('isDateValid valid cases', () => {
${'01/Mar/2020'} | ${true}
${'1.1.1'} | ${false}
`('should return $expected when input is: $input', ({ input, expected }) => {
expect(isDateValid(input)).toBe(expected);
expect(isStringDate(input)).toBe(expected);
});
});
10 changes: 5 additions & 5 deletions src/is-date-string.ts → src/is-string-date.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* Determines whether the input string is valid date or not.
* @param value - input string.
* @param input - input string.
* @returns true if the input string is valid date, false otherwise.
* @example
* ```ts
* isDateString('01/01/2000') // true
* isDateString('31/09/2000') // false
* isStringDate('01/01/2000') // true
* isStringDate('31/09/2000') // false
* ```
* @remarks
* While there are some regular expressions that allow date validations,
Expand All @@ -14,8 +14,8 @@
*
* @public
*/
export function isDateString(value: string): value is string {
export function isStringDate(input: string): input is string {
const regexp =
/(^(?:(?:(?:31(?:(?:([-.\/])(?:0?[13578]|1[02])\2)|(?:([-.\/ ])(?:Jan|JAN|Mar|MAR|May|MAY|Jul|JUL|Aug|AUG|Oct|OCT|Dec|DEC)\3)))|(?:(?:29|30)(?:(?:([-.\/])(?:0?[13-9]|1[0-2])\4)|(?:([-.\/ ])(?:Jan|JAN|Mar|MAR|Apr|APR|May|MAY|Jun|JUN|Jul|JUL|Aug|AUG|Sep|SEP|Oct|OCT|Nov|NOV|Dec|DEC)\5))))(?:(?:1[6-9]|[2-9]\d)?\d{2}))$|^(?:29(?:(?:([-.\/])(?:0?2)\6)|(?:([-.\/ ])(?:Feb|FEB)\7))(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))$|^(?:(?:0?[1-9]|1\d|2[0-8])(?:(?:([-.\/])(?:(?:0?[1-9]|(?:1[0-2])))\8)|(?:([-.\/ ])(?:Jan|JAN|Feb|FEB|Mar|MAR|May|MAY|Jul|JUL|Aug|AUG|Oct|OCT|Dec|DEC)\9))(?:(?:1[6-9]|[2-9]\d)?\d{2}))$)/gm;
return regexp.test(value);
return regexp.test(input);
}
2 changes: 1 addition & 1 deletion src/is-string.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isString } from '.';
import { isString } from './is-string';

describe('isString cases', () => {
test.each`
Expand Down
6 changes: 3 additions & 3 deletions src/is-string.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Determines whether the input is string or not.
* @param value - input
* @param input - input
* @returns true if the input is string, false otherwise.
* @example
* ```ts
Expand All @@ -11,6 +11,6 @@
* ```
* @public
*/
export function isString(value: unknown): value is string {
return typeof value === 'string';
export function isString(input: unknown): input is string {
return typeof input === 'string';
}
2 changes: 1 addition & 1 deletion src/to-chunks.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { toChunks } from './';
import { toChunks } from './to-chunks';

describe('toChunks cases', () => {
test.each`
Expand Down
8 changes: 4 additions & 4 deletions src/to-chunks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Split a string into N chunks of equal size.
* The last chunk may be smaller.
* @param value - string
* @param input - string
* @param chunkSize - number
* @returns Array of chunks or an empty array.
* @example
Expand All @@ -15,8 +15,8 @@
* ```
* @beta
*/
export function toChunks(value: string, chunkSize: number): string[] {
const chunkCount = Math.ceil(value.length / chunkSize);
export function toChunks(input: string, chunkSize: number): string[] {
const chunkCount = Math.ceil(input.length / chunkSize);
const regexp = new RegExp(`.{1,${chunkCount}}`, 'g');
return value.match(regexp) ?? [];
return input.match(regexp) ?? [];
}
6 changes: 3 additions & 3 deletions src/wrap-in-array.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Wraps the input in an array even if the input is an array.
* @param value - input
* @param input - input
* @returns wrapped input in an array.
* @example
* ```ts
Expand All @@ -10,6 +10,6 @@
* ```
* @public
*/
export function wrapInArray<Type>(value: Type): Type[] {
return [value];
export function wrapInArray<Input>(input: Input): Input[] {
return [input];
}

0 comments on commit 829126b

Please sign in to comment.