Skip to content

Commit

Permalink
Move query and queryAll to queries namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
demiazz committed May 4, 2017
1 parent 822b650 commit e5dcc56
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 38 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"optionalDependencies": false,
"peerDependencies": false,
}],
"import/prefer-default-export": "off",
"no-param-reassign": "off",
"prefer-spread": "off",
"prefer-rest-params": "off"
Expand Down
14 changes: 5 additions & 9 deletions spec/queryAll.spec.js → spec/queries/query-all.spec.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { queryAll } from "../src";
import { queryAll } from "../../src";

describe("queryAll", () => {
afterEach(clearFixtures);

it("returns array of childs which matched by selector", () => {
useFixture(
`
useFixture(`
<div class="root" >
<div class="matched"></div>
<div class="matched"></div>
<div class="notMatched"></div>
</div>
<div class="matched"></div>
<div class="matched"></div>
`
);
`);

const subject = document.querySelector(".root");

Expand All @@ -24,15 +22,13 @@ describe("queryAll", () => {
});

it("returns empty array when children which matched by selector not existed", () => {
useFixture(
`
useFixture(`
<div class="root" >
<div class="notMatched"></div>
</div>
<div class="matched"></div>
<div class="matched"></div>
`
);
`);

const subject = document.querySelector(".root");

Expand Down
14 changes: 5 additions & 9 deletions spec/query.spec.js → spec/queries/query.spec.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
import { query } from "../src";
import { query } from "../../src";

describe("query", () => {
afterEach(clearFixtures);

it("returns first child which matched by selector", () => {
useFixture(
`
useFixture(`
<div class="root" >
<div class="matched"></div>
<div class="other"></div>
</div>
<div class="matched"></div>
`
);
`);

const subject = document.querySelector(".root");

expect(query(subject, ".matched")).toBe(subject.querySelector(".matched"));
});

it("returns null when child which matched by selector not exists", () => {
useFixture(
`
useFixture(`
<div class="root" >
<div class="notMatched"></div>
</div>
<div class="matched"></div>
`
);
`);

const subject = document.querySelector(".root");

Expand Down
26 changes: 6 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
/* @flow */

import type { Elements, Selector } from "./types";

import { html, body } from "./aliases";
import query from "./queries/query";
import queryAll from "./queries/query-all";

type CSSClass = string;
/* Types */

type Elements = Array<Element>;
type CSSClass = string;

type EventDetails = { [key: string]: mixed };

Expand All @@ -14,26 +18,8 @@ type EventType = string;

type PredicateFn = (element: Element) => boolean;

type Selector = string;

/* Utilities */

function toArray(arrayLike: any): Array<any> {
return Array.prototype.slice.call(arrayLike);
}

/* Queries */

function query(element: Element, selector: Selector): ?Element {
return element.querySelector(selector);
}

function queryAll(element: Element, selector: Selector): Elements {
const elements = element.querySelectorAll(selector);

return (toArray(elements): Elements);
}

type MatchesFn = (selector: Selector) => boolean;

function getMatchesFn(): MatchesFn {
Expand Down
13 changes: 13 additions & 0 deletions src/queries/query-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* @flow */

import type { Elements, Selector } from "../types";

import { toArray } from "../utils";

function queryAll(element: Element, selector: Selector): Elements {
const elements = element.querySelectorAll(selector);

return (toArray(elements): Elements);
}

export default queryAll;
9 changes: 9 additions & 0 deletions src/queries/query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* @flow */

import type { Selector } from "../types";

function query(element: Element, selector: Selector): ?Element {
return element.querySelector(selector);
}

export default query;
5 changes: 5 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* @flow */

export type Elements = Array<Element>;

export type Selector = string;
7 changes: 7 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* @flow */

function toArray(arrayLike: any): Array<any> {
return Array.prototype.slice.call(arrayLike);
}

export { toArray };

0 comments on commit e5dcc56

Please sign in to comment.