Skip to content

Commit

Permalink
Add hasAttr function
Browse files Browse the repository at this point in the history
  • Loading branch information
demiazz committed Jun 11, 2017
1 parent 0a519cf commit b30b877
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions d.ts/homey.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export function queryAll(element: Element, selector: CSSSelector): Elements;
export function closest(element: Element, condition: CSSSelector | Predicate): Element | null;
export function getHtml(element: Element): string;
export function getText(element: Node): string;
export function hasAttr(element: Element, attribute: string): boolean;
export function matches(element: Element, selector: CSSSelector): boolean;
export function parent(element: Element): Element | null;
export function parentBy(element: Element, condition: CSSSelector | Predicate): Element | null;
Expand Down
21 changes: 21 additions & 0 deletions spec/traversing/has-attr.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { hasAttr } from "../../src";

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

it("returns true if element has a given attribute", () => {
useFixture(`<div class="root"></div>`);

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

expect(hasAttr(subject, "class")).toBe(true);
});

it("returns false if element hasn't a given attribute", () => {
useFixture(`<div class="root"></div>`);

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

expect(hasAttr(subject, "id")).toBe(false);
});
});
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ import setText from "./manipulation/set-text";
import query from "./quering/query";
import queryAll from "./quering/query-all";
import closest from "./traversing/closest";
import getAttr from "./traversing/get-attr";
import getHtml from "./traversing/get-html";
import getText from "./traversing/get-text";
import hasAttr from "./traversing/has-attr";
import matches from "./traversing/matches";
import parent from "./traversing/parent";
import parentBy from "./traversing/parent-by";
Expand Down Expand Up @@ -81,8 +83,10 @@ export {
query,
queryAll,
closest,
getAttr,
getHtml,
getText,
hasAttr,
matches,
parent,
parentBy,
Expand Down
7 changes: 7 additions & 0 deletions src/traversing/has-attr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* @flow */

function hasAttr(element: Element, attribute: string): boolean {
return element.hasAttribute(attribute);
}

export default hasAttr;

0 comments on commit b30b877

Please sign in to comment.