Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

is-older

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

is-older

Compare the age of two elements based on their position in the DOM

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

100% test coverage

is-older

Compare the age of two elements based on their position in the DOM

Compares two elements to see which is "older" (higher up in the DOM). Age is determined by the number of ancestors each element has. Can be passed as a sort callback when using Array.sort

Installation

Install via npm.

$ npm i is-older --save

Usage

Require and call.

<div>
  <div id="two">
    <p id="one"></p>
  </div>
  <p id="three"></p>
</div>
var isOlder = require('is-older');

var one = document.getElementById('one');
var two = document.getElementById('two');
var three = document.getElementById('three');

console.log(isOlder(two, one));   // returns 1
console.log(isOlder(one, two));   // returns -1
console.log(isOlder(one, three)); // returns 0

Sorting

You can pass as a callback to Array.sort to arrange a collection of elements by their age:

var els = document.querySelector('*');

console.log(els.sort(isOlder)).reverse();
// `els` is sorted from oldest to youngest

console.log(els.sort(isOlder));
// `els` is sorted from youngest to oldest

FAQs

Package last updated on 10 Feb 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts