From c5fda8c22f004b6f94562b495a19ac7bc7240902 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucien=20B=C3=A9ni=C3=A9?= Date: Wed, 13 Jun 2018 21:00:36 -0400 Subject: [PATCH] feat(typings): provides better intellisense refactored the code to provide better intellisense when using in a typescript file --- __tests__/qselect.test.js | 16 ++++++++-------- lib/index.ts | 21 +++++++++++++++------ 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/__tests__/qselect.test.js b/__tests__/qselect.test.js index d87db07..cc704f9 100644 --- a/__tests__/qselect.test.js +++ b/__tests__/qselect.test.js @@ -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', () => { @@ -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', () => { @@ -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', () => { diff --git a/lib/index.ts b/lib/index.ts index ac1682d..425b96f 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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(el); const isNan = isNanOrInfinite(el, selector); + if (!root) { + return false; + } + if (isNan && isNan.toString().length !== 0) { throw new Error(errorSelector); } else if (isSingle) { @@ -36,10 +41,14 @@ const errorHanler = (isSingle: boolean, { el, selector }: { el: any, selector: a }; -const core = (type: boolean, ...args: Array): HTMLElement | Array | Error => { +const core = (type: boolean, ...args: Array) => { const isSingle = type === queryType.single; const [el, selector] = args; - const root = document.querySelector(el); + const root = document.querySelector(el || ''); + + if (!root || !el) { + throw new Error(errorSelector); + } if (errorHanler(isSingle, { el, selector })) { if (selector) { @@ -51,9 +60,9 @@ const core = (type: boolean, ...args: Array): HTMLElement | Array 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 { $,