Skip to content

Commit

Permalink
feat(typings): provides better intellisense
Browse files Browse the repository at this point in the history
refactored the code to provide better intellisense when using in a typescript file
  • Loading branch information
lbenie committed Jun 14, 2018
1 parent 4acabe8 commit c5fda8c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
16 changes: 8 additions & 8 deletions __tests__/qselect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ describe('qselect', () => {
it('should throw if el is undefined', () => {
expect(() => {
$(undefined);
}).toThrowError(errorSelector);
}).toThrowError(invalidSelector);
});

it('should be null if el is null', () => {
expect(() => {
$(null);
}).toThrowError(errorSelector);
}).toThrowError(invalidSelector);
});

it('should throw if el is Nan', () => {
expect(() => {
$(NaN);
}).toThrowError(errorSelector);
}).toThrowError(invalidSelector);
});

it('should throw if el is Infinity', () => {
Expand Down Expand Up @@ -215,19 +215,19 @@ describe('qselect', () => {
it('should throw if el is undefined', () => {
expect(() => {
$$(undefined);
}).toThrowError(errorSelector);
}).toThrowError(invalidSelector);
});

it('should throw if el is null', () => {
expect(() => {
$$(null);
}).toThrowError(errorSelector);
}).toThrowError(invalidSelector);
});

it('should throw if el is Nan', () => {
expect(() => {
$$(NaN);
}).toThrowError(errorSelector);
}).toThrowError(invalidSelector);
});

it('should throw if el is Infinity', () => {
Expand Down Expand Up @@ -299,13 +299,13 @@ describe('qselect', () => {
it('should throw if el is undefined and selector is undefined', () => {
expect(() => {
$$(undefined, undefined);
}).toThrowError(errorSelector);
}).toThrowError(invalidSelector);
});

it('should throw if el is null and selector is null', () => {
expect(() => {
$$(null, null);
}).toThrowError(errorSelector);
}).toThrowError(invalidSelector);
});

it('should be a node list if el exists and selector is undefined', () => {
Expand Down
21 changes: 15 additions & 6 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ const isNanOrInfinite = (el: any, selector: any) => {
return result.some(x => x);
};

const errorHanler = (isSingle: boolean, { el, selector }: { el: any, selector: any }): boolean => {
const root = document.querySelector(el);
const errorHanler = (isSingle: boolean, { el, selector }:
{ el: string, selector: string | undefined }): boolean => {
const root = document.querySelector<HTMLElement>(el);
const isNan = isNanOrInfinite(el, selector);

if (!root) {
return false;
}

if (isNan && isNan.toString().length !== 0) {
throw new Error(errorSelector);
} else if (isSingle) {
Expand All @@ -36,10 +41,14 @@ const errorHanler = (isSingle: boolean, { el, selector }: { el: any, selector: a
};


const core = (type: boolean, ...args: Array<any>): HTMLElement | Array<HTMLElement> | Error => {
const core = (type: boolean, ...args: Array<string | undefined>) => {
const isSingle = type === queryType.single;
const [el, selector] = args;
const root = document.querySelector(el);
const root = document.querySelector<HTMLElement>(el || '');

if (!root || !el) {
throw new Error(errorSelector);
}

if (errorHanler(isSingle, { el, selector })) {
if (selector) {
Expand All @@ -51,9 +60,9 @@ const core = (type: boolean, ...args: Array<any>): HTMLElement | Array<HTMLEleme
throw new Error(errorSelector);
};

const $ = (el: any, selector?: any) => core(queryType.single, el, selector);
const $ = (el: string, selector?: string) => core(queryType.single, el, selector);

const $$ = (el: any, selector?: any) => core(queryType.multiple, el, selector);
const $$ = (el: string, selector?: string) => core(queryType.multiple, el, selector);

export {
$,
Expand Down

0 comments on commit c5fda8c

Please sign in to comment.