-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
653 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
declare const $: (el: any, selector?: any) => HTMLElement | Error | HTMLElement[]; | ||
declare const $$: (el: any, selector?: any) => HTMLElement | Error | HTMLElement[]; | ||
export { $, $$, }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import isDom from 'is-dom'; | ||
|
||
const errorSelector = 'Selector does not exists in the DOM'; | ||
|
||
interface IQueryType { | ||
single: boolean; | ||
multiple: boolean; | ||
} | ||
|
||
const queryType = { | ||
single: true, | ||
multiple: false, | ||
}; | ||
|
||
const isNanOrInfinite = (el: any, selector: any) => { | ||
const result = [ | ||
Number.isNaN(el), | ||
Number.isNaN(selector), | ||
Number.isFinite(el), | ||
Number.isFinite(selector), | ||
]; | ||
|
||
return result.some(x => x); | ||
}; | ||
|
||
const errorHanler = (isSingle: boolean, { el, selector }: { el: any, selector: any }): boolean => { | ||
const root = document.querySelector(el); | ||
const isNan = isNanOrInfinite(el, selector); | ||
|
||
if (isNan && isNan.toString().length !== 0) { | ||
throw new Error(errorSelector); | ||
} else if (isSingle) { | ||
return selector ? | ||
isDom(root.querySelector(selector)) : | ||
isDom(root); | ||
} | ||
|
||
return selector ? | ||
isDom(...root.querySelectorAll(selector)) : | ||
isDom(...Array.from(document.querySelectorAll(el))); | ||
}; | ||
|
||
|
||
const core = (type: boolean, ...args: Array<any>): HTMLElement | Array<HTMLElement> | Error => { | ||
const isSingle = type === queryType.single; | ||
const [el, selector] = args; | ||
const root = document.querySelector(el); | ||
|
||
if (errorHanler(isSingle, { el, selector })) { | ||
if (selector) { | ||
return isSingle ? root.querySelector(selector) : [...root.querySelectorAll(selector)]; | ||
} | ||
return isSingle ? root : Array.from(document.querySelectorAll(el)); | ||
} | ||
|
||
throw new Error(errorSelector); | ||
}; | ||
|
||
|
||
const $ = (el: any, selector?: any) => core(queryType.single, el, selector); | ||
|
||
const $$ = (el: any, selector?: any) => core(queryType.multiple, el, selector); | ||
|
||
export { | ||
$, | ||
$$, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
declare module 'is-dom'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2015", | ||
"module": "es2015", | ||
"strict": true, | ||
"moduleResolution": "node", | ||
"allowSyntheticDefaultImports": true, | ||
"sourceMap": true, | ||
"declaration": true, | ||
"lib": [ | ||
"es6", | ||
"dom", | ||
"dom.iterable", | ||
"scripthost", | ||
], | ||
"baseUrl": "." | ||
}, | ||
"include": [ | ||
"lib/**/*.ts", | ||
], | ||
"exclude": [ | ||
"node_modules" | ||
] | ||
} |
Oops, something went wrong.