Skip to content

Commit

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

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

describe("when selector is given", () => {
it("returns all parents which matches by selector", () => {
useFixture(
`
useFixture(`
<div class="outer-wrapper">
<div class="parent">
<div class="inner-wrapper">
<div class="root"></div>
</div>
</div>
</div>
`
);
`);

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

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

it("returns empty array if element hasn't any parent which matches by selector", () => {
useFixture(
`
useFixture(`
<div class="outer-wrapper">
<div class="inner-wrapper">
<div class="root"></div>
</div>
</div>
`
);
`);

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

Expand All @@ -43,17 +39,15 @@ describe("parentsBy", () => {

describe("when predicate is given", () => {
it("returns all parents which matches by predicate", () => {
useFixture(
`
useFixture(`
<div class="outer-wrapper">
<div class="parent">
<div class="inner-wrapper">
<div class="root"></div>
</div>
</div>
</div>
`
);
`);

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

it("returns empty array if element hasn't any parent which matches by predicate", () => {
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
14 changes: 2 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* @flow */

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

import { html, body } from "./aliases";
import query from "./queries/query";
Expand All @@ -9,6 +9,7 @@ import matches from "./traversing/matches";
import parent from "./traversing/parent";
import parentBy from "./traversing/parent-by";
import parents from "./traversing/parents";
import parentsBy from "./traversing/parents-by";

/* Types */

Expand Down Expand Up @@ -99,17 +100,6 @@ function dataset(element: HTMLElement): Dataset {
* Traverse
*/

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

return parents(element).filter(predicate);
}

type ClosestFn = (element: Element, selector: Selector) => ?Element;

function polyfillClosest(element: Element, selector: Selector): ?Element {
Expand Down
19 changes: 19 additions & 0 deletions src/traversing/parents-by.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* @flow */

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

import matches from "./matches";
import parents from "./parents";

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

return parents(element).filter(predicate);
}

export default parentsBy;

0 comments on commit 827c9ae

Please sign in to comment.