Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finally!!!! A starting point for adding test suite in Intent. #26

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 13 additions & 10 deletions packages/core/lib/console/consoleIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ConsoleLogger } from './logger';
export class ConsoleIO {
schema: ArgumentParserOutput;
rawValues: Record<string, any>;
values: Record<string, any> = { arguments: {}, options: {} };
values = { arguments: {}, options: {} };
hasErrors: boolean;
missingArguments: string[];

Expand Down Expand Up @@ -36,6 +36,14 @@ export class ConsoleIO {
return this.values.options[key];
}

arguments(): Record<string, any> {
return this.values.arguments;
}

options(): Record<string, any> {
return this.values.options;
}

handle(): ConsoleIO {
this.schema = ArgumentParser.from(this.schemaString);
this.values = { arguments: {}, options: {} };
Expand Down Expand Up @@ -68,16 +76,11 @@ export class ConsoleIO {
this.validateArguments();
if (this.hasErrors) return this;

/**
* Parse options
*/
for (const option of this.schema.options) {
const value = Obj.get(this.argv, option.name, ...option.alias);
if (value) {
this.values.options[option.name] = value;
} else {
this.values.options[option.name] = option.defaultValue;
}
const values = Object.values(
Obj.pick(this.argv, [option.name, ...option.alias]),
);
this.values.options[option.name] = values[0] ? values[0] : undefined;
}

return this;
Expand Down
17 changes: 17 additions & 0 deletions packages/core/lib/utils/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,21 @@ export class Arr {

return undefined;
}

static intersect<T = string | number, M = T>(
arr1: T[],
arr2: M[],
): Array<T | M> {
const tempMap = new Map<T | M, number>();
const newArr = [] as Array<T | M>;
for (const val of arr1) {
tempMap.set(val, 1);
}

for (const val2 of arr2) {
if (tempMap.has(val2)) newArr.push(val2);
}

return newArr;
}
}
24 changes: 5 additions & 19 deletions packages/core/lib/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,14 @@ import { join } from 'path';
import { findProjectRoot } from './path';

export const isEmpty = (value: any) => {
if (Str.isString(value)) {
return value === '';
}

if (Arr.isArray(value)) {
return !value.length;
}

if (Obj.isObj(value)) {
return Obj.isEmpty(value);
}

if (Number.isNaN(value) || value === undefined) {
return true;
}

if (Str.isString(value)) return value === '';
if (Arr.isArray(value)) return !value.length;
if (Obj.isObj(value)) return Obj.isEmpty(value);
if (Number.isNaN(value) || value === undefined) return true;
return false;
};

export const isBoolean = (value: any): boolean => {
return typeof value === 'boolean';
};
export const isBoolean = (value: any): boolean => typeof value === 'boolean';

export const toBoolean = (value: any) => {
const val = String(value);
Expand Down
26 changes: 12 additions & 14 deletions packages/core/lib/utils/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,26 +102,24 @@ export class Obj {
return !Obj.isEmpty(obj);
}

static get<T>(
static get<T = any>(
obj: Record<string, any>,
key: string,
...aliasKeys: string[]
defaultValue?: T,
): T {
const keys = [key, ...aliasKeys];
for (const key of keys) {
if (key in obj) return obj[key];
const splitKeys = key.split('.');
if (!splitKeys.length) return;
if (key in obj) return obj[key];
const splitKeys = key.split('.');
if (!splitKeys.length) return;

if (Arr.isArray(obj[splitKeys[0]])) {
return Arr.get(obj[splitKeys[0]], splitKeys.slice(1).join('.'));
}
if (Arr.isArray(obj[splitKeys[0]])) {
return Arr.get(obj[splitKeys[0]], splitKeys.slice(1).join('.'));
}

if (Obj.isObj(obj[splitKeys[0]])) {
return Obj.get(obj[splitKeys[0]], splitKeys.slice(1).join('.'));
}
if (Obj.isObj(obj[splitKeys[0]])) {
return Obj.get(obj[splitKeys[0]], splitKeys.slice(1).join('.'));
}
return undefined;

return defaultValue;
}

static sort<T = Record<string, any>>(obj: T): T {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/lib/utils/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export class Str {
return !Str.isString(value);
};

static len = (str: string): number => {
static len = (str?: string): number => {
return str?.length ?? 0;
};

Expand Down
91 changes: 91 additions & 0 deletions packages/core/tests/helpers/arrayHelper.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { Arr } from '../../lib';

jest.mock('../../lib/config/service');

describe('Array Helper', () => {
beforeEach(async () => {});


it('should return object', () => {
const arr = [
2,
['bar', 'piyush', 'intent'],
{ foo: 'bar' },
[{ bar: 'foo' }],
];

const keys = ['foo', 'chhabra', 'best framework', 'obj'];
expect(Arr.toObj(arr, keys)).toStrictEqual([
{ foo: 'bar', chhabra: 'piyush', 'best framework': 'intent' },
{ foo: { bar: 'foo' } },
]);
});

// it('should throw exception', () => {
// const arr = {};
// expect(Arr.isArray(arr as [], true)).toThrow(InvalidValue);
// });

it('should return false', () => {
const arr = {};
expect(Arr.isArray(arr as [])).toBeFalsy();
});

it('should return false', () => {
const arr = [];
expect(Arr.isArray(arr)).toBeTruthy();
});

it('should return array of nested members flattened', () => {
const arr = [1, [2, 3, 4], [[5, 6], [[7], 8], 9]];
expect(Arr.collapse(arr)).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]);
});

it('should return array with random order of input', () => {
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const mockMath = Object.create(global.Math);
mockMath.random = () => 0.5;
global.Math = mockMath;
expect(Arr.random(arr)).toStrictEqual([1, 6, 2, 8, 3, 7, 4, 9, 5]);
});

it('should return array with sorted order of input', () => {
const arr = [6, 1, 2, 8, 3, 7, 4, 9, 5];
expect(Arr.sort(arr)).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]);
});

it('should return array with descending order of input', () => {
const arr = [6, 1, 2, 8, 3, 7, 4, 9, 5];
expect(Arr.sortDesc(arr)).toStrictEqual([9, 8, 7, 6, 5, 4, 3, 2, 1]);
});

it('should return array with common elements from both input arrays', () => {
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [4, 5, 6, 7, 8];
expect(Arr.intersect(arr1, arr2)).toStrictEqual([4, 5]);
});

it('should return array with elements keys mentioned to pick', () => {
const arr = [
{ developer: { id: 1, name: 'Taylor' } },
{ developer: { id: 2, name: 'Abigail' } },
];
const pick = ['*.developer.name'];
expect(Arr.pick(arr, pick)).toStrictEqual([
{ developer: { name: 'Taylor' } },
{ developer: { name: 'Abigail' } },
]);
});

it('should return array with all elements except the keys mentioned', () => {
const arr = [
{ developer: { id: 1, name: 'Taylor' } },
{ developer: { id: 2, name: 'Abigail' } },
];
const pick = ['*.developer.name'];
expect(Arr.except(arr, pick)).toStrictEqual([
{ developer: { id: 1 } },
{ developer: { id: 2 } },
]);
});
});
Loading