Skip to content

Commit

Permalink
feat: add support for typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
lbenie committed Jun 1, 2018
1 parent fbcdb85 commit 9d8dc17
Show file tree
Hide file tree
Showing 8 changed files with 653 additions and 41 deletions.
9 changes: 8 additions & 1 deletion gulpfile.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import runSequence from 'run-sequence';
import fs from 'fs';
import github from 'conventional-github-releaser';
import dotenv from 'dotenv';
import ts from 'gulp-typescript';

const project = ts.createProject('tsconfig.json');

dotenv.config();

Expand All @@ -17,6 +20,10 @@ const opts = minimist(process.argv.slice(2), {
semver: process.env.SEMVER || 'patch',
});

gulp.task('typescript', () => gulp
.src('lib/**/*.ts')
.pipe(project())
.pipe(gulp.dest('lib')));

gulp.task('changelog', () => gulp
.src('CHANGELOG.md', { buffer: false })
Expand Down Expand Up @@ -55,7 +62,7 @@ gulp.task('create-new-tag', done =>
}));

gulp.task('release', done =>
runSequence('bump-version', 'changelog', 'commit-changelog', 'push-changes', 'create-new-tag', 'github-release', (err) => {
runSequence('typescript', 'bump-version', 'changelog', 'commit-changelog', 'push-changes', 'create-new-tag', 'github-release', (err) => {
if (err) {
log.error(err.message);
} else {
Expand Down
3 changes: 3 additions & 0 deletions lib/index.d.ts
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 { $, $$, };
27 changes: 6 additions & 21 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,41 @@ const queryType = {
single: true,
multiple: false,
};

const isNanOrInfinite = (el, selector) => {
const result = [
Number.isNaN(el),
Number.isNaN(selector),
Number.isFinite(el),
Number.isFinite(selector),
];

return result.find(x => x);
return result.some(x => x);
};

const errorHanler = (isSingle, { el, selector }) => {
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(...document.querySelectorAll(el));
isDom(...Array.from(document.querySelectorAll(el)));
};


const core = (type, ...args) => {
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 : [...document.querySelectorAll(el)];
return isSingle ? root : Array.from(document.querySelectorAll(el));
}

throw new Error(errorSelector);
};

const $ = (el, selector = undefined) => core(queryType.single, el, selector);

const $$ = (el, selector = undefined) => core(queryType.multiple, el, selector);

export {
$,
$$,
};
const $ = (el, selector) => core(queryType.single, el, selector);
const $$ = (el, selector) => core(queryType.multiple, el, selector);
export { $, $$ };
67 changes: 67 additions & 0 deletions lib/index.ts
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 {
$,
$$,
};
1 change: 1 addition & 0 deletions lib/shim.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'is-dom';
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"files": [
"lib/index.js"
],
"types": [
"lib/index.d.ts"
],
"bugs": {
"url": "https://github.com/lbenie/qselector/issues"
},
Expand Down Expand Up @@ -65,6 +68,7 @@
"gulp-bump": "^3.1.1",
"gulp-conventional-changelog": "^1.1.24",
"gulp-git": "^2.5.2",
"gulp-typescript": "^4.0.2",
"husky": "^1.0.0-rc.2",
"jest": "^22.4.3",
"lint-staged": "^7.0.5",
Expand All @@ -76,7 +80,9 @@
"stryker-baseline-reporter": "^1.0.3",
"stryker-html-reporter": "^0.13.3",
"stryker-javascript-mutator": "^0.6.3",
"stryker-jest-runner": "^0.6.0"
"stryker-jest-runner": "^0.6.0",
"ts-jest": "^22.4.6",
"typescript": "^2.9.1"
},
"engines": {
"node": ">=8"
Expand Down
24 changes: 24 additions & 0 deletions tsconfig.json
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"
]
}
Loading

0 comments on commit 9d8dc17

Please sign in to comment.