Skip to content

Commit

Permalink
Move parentBy to traversing namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
demiazz committed May 4, 2017
1 parent 1d27568 commit 880a3eb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 38 deletions.
26 changes: 9 additions & 17 deletions spec/parentBy.spec.js → spec/traversing/parent-by.spec.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { matches, parentBy } from "../src";
import { matches, parentBy } from "../../src";

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

describe("when selector given", () => {
it("returns parent element which matches by selector", () => {
useFixture(
`
useFixture(`
<div class="parent">
<div class="wrapper">
<div class="root"></div>
</div>
</div>
`
);
`);

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

Expand All @@ -23,15 +21,13 @@ describe("parentBy", () => {
});

it("returns null if parent element which matches doesn't exists", () => {
useFixture(
`
useFixture(`
<div class="outer-wrapper">
<div class="inner-wrapper">
<div class="root"></div>
</div>
</div>
`
);
`);

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

Expand All @@ -41,15 +37,13 @@ describe("parentBy", () => {

describe("when predicate function is given", () => {
it("returns parent element which matches by predicate", () => {
useFixture(
`
useFixture(`
<div class="parent">
<div class="wrapper">
<div class="root"></div>
</div>
</div>
`
);
`);

const subject = document.querySelector(".root");
const predicate = e => matches(e, ".parent");
Expand All @@ -60,15 +54,13 @@ describe("parentBy", () => {
});

it("returns null if parent element which matches doesn't exists", () => {
useFixture(
`
useFixture(`
<div class="outer-wrapper">
<div class="inner-wrapper">
<div class="root"></div>
</div>
</div>
`
);
`);

const subject = document.querySelector(".root");
const predicate = e => matches(e, ".parent");
Expand Down
22 changes: 1 addition & 21 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import query from "./queries/query";
import queryAll from "./queries/query-all";
import matches from "./traversing/matches";
import parent from "./traversing/parent";
import parentBy from "./traversing/parent-by";

/* Types */

Expand Down Expand Up @@ -97,27 +98,6 @@ function dataset(element: HTMLElement): Dataset {
* Traverse
*/

function parentBy(
element: Element,
condition: PredicateFn | Selector
): ?Element {
const predicate = typeof condition === "string"
? e => matches(e, condition)
: condition;

let current = parent(element);

while (current) {
if (predicate(current)) {
return current;
}

current = parent(current);
}

return null;
}

function parents(element: Element): Elements {
const result = [];

Expand Down
26 changes: 26 additions & 0 deletions src/traversing/parent-by.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* @flow */

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

import matches from "./matches";
import parent from "./parent";

function parentBy(element: Element, condition: Predicate | Selector): ?Element {
const predicate = typeof condition === "string"
? e => matches(e, condition)
: condition;

let current = parent(element);

while (current) {
if (predicate(current)) {
return current;
}

current = parent(current);
}

return null;
}

export default parentBy;
2 changes: 2 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

export type Elements = Array<Element>;

export type Predicate = (element: Element) => boolean;

export type Selector = string;

0 comments on commit 880a3eb

Please sign in to comment.