Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tablesort

Package Overview
Dependencies
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tablesort - npm Package Compare versions

Comparing version 1.1.2 to 1.2.2

demo/prettify.js

2

package.json
{
"name": "tablesort",
"description": "tablesort BRAP tablesort BRAP",
"version": "1.1.2",
"version": "1.2.2",
"author": "tristen <@fallsemo>",

@@ -6,0 +6,0 @@ "main": "./tablesort.js",

@@ -13,4 +13,11 @@ # tablesort.js

```
## Features
* Sort strings
* Sort numbers
* Sort currency
## Additional options
__1. Ascending/Descending__
You can pass in options as a second parameter. Currently one option is supported: `descending: true`. By default, sort is set to ascending.

@@ -24,8 +31,8 @@

## Features
__2. Exclude columns__
For columns that do not require sorting, you can add a class of `no-sort`
``` html
<th class='no-sort'>Name</th>
```
* Sort strings
* Sort numbers
* Sort currency
## Ender support

@@ -43,2 +50,36 @@

## Default style
Add the styling below to your CSS or roll with your own.
``` css
th.sort-header {
cursor:pointer;
}
th.sort-header::-moz-selection,
th.sort-header::selection {
background:transparent;
}
table th.sort-header:after {
content:'';
float:right;
margin-top:7px;
border-width:4px 4px 0;
border-style:solid;
border-color:#404040 transparent;
visibility:hidden;
}
table th.sort-header:hover:after {
visibility:visible;
}
table th.sort-up:after,
table th.sort-down:after,
table th.sort-down:hover:after {
visibility:visible;
opacity:0.4;
}
table th.sort-up:after {
border-width:0 4px 4px;
}
```
## Building

@@ -59,3 +100,2 @@

- Choose which row to begin sorting on.
- Column exclusion
- EventListener to rebuild the table.

@@ -29,17 +29,19 @@ // tablesort.js

var cell = firstRow.cells[i];
cell.className += ' sort-header';
this.addEvent(cell, 'click', function(e) {
// Delete any sort classes on table headers that are not the current one.
var siblings = that.getParent(cell, 'tr').getElementsByTagName('th');
for (var i = 0; i < siblings.length; i++) {
if (that.hasClass(siblings[i], 'sort-up') || that.hasClass(siblings[i], 'sort-down')) {
if (siblings[i] !== this) {
siblings[i].className = siblings[i].className
.replace(' sort-down', '')
.replace(' sort-up', '');
if (!that.hasClass(cell, 'no-sort')) {
cell.className += ' sort-header';
this.addEvent(cell, 'click', function(e) {
// Delete any sort classes on table headers that are not the current one.
var siblings = that.getParent(cell, 'tr').getElementsByTagName('th');
for (var i = 0; i < siblings.length; i++) {
if (that.hasClass(siblings[i], 'sort-up') || that.hasClass(siblings[i], 'sort-down')) {
if (siblings[i] !== this) {
siblings[i].className = siblings[i].className
.replace(' sort-down', '')
.replace(' sort-up', '');
}
}
}
}
that.sortTable(this);
});
that.sortTable(this);
});
}
}

@@ -61,3 +63,3 @@ },

return -1;
}
};
var sortNumber = function(a, b) {

@@ -71,3 +73,3 @@ var aa = that.getInnerText(a.cells[that.col]);

return that.compareNumber(bb, aa);
}
};

@@ -89,3 +91,3 @@ // Work out a type for the column

// Sort as number if a currency key exists or number
if (item.match(/^-?[£$€Û¢´]\d/) || item.match(/^-?(\d+[,\.]?)+(E[-+][\d]+)?%?$/)) {
if (item.match(/^-?[£$€Û¢´]\d/) || item.match(/^-?(\d+[,\.]?)+(E[\-+][\d]+)?%?$/)) {
sortFunction = sortNumber;

@@ -96,13 +98,13 @@ } else {

this.col = column;
var firstRow = [], newRows = [];
var firstRow = [], newRows = [], k, j;
for (var k = 0; k < t.tBodies.length; k++) {
for (var i = 0; i < t.tBodies[k].rows[0].length; i++) {
for (k = 0; k < t.tBodies.length; k++) {
for (i = 0; i < t.tBodies[k].rows[0].length; i++) {
firstRow[i] = t.tBodies[k].rows[0][i];
}
}
for (var k = 0; k < t.tBodies.length; k++) {
for (k = 0; k < t.tBodies.length; k++) {
if (!that.thead) {
// skip the first row
for (var j = 1; j < t.tBodies[k].rows.length; j++) {
for (j = 1; j < t.tBodies[k].rows.length; j++) {
newRows[j-1] = t.tBodies[k].rows[j];

@@ -112,3 +114,3 @@ }

// don't skip the first row
for (var j = 0; j < t.tBodies[k].rows.length; j++) {
for (j = 0; j < t.tBodies[k].rows.length; j++) {
newRows[j] = t.tBodies[k].rows[j];

@@ -142,3 +144,3 @@ }

// append rows that already exist rather than creating new ones
for (var i = 0; i < newRows.length; i++) {
for (i = 0; i < newRows.length; i++) {
if (!newRows[i].className) {

@@ -178,6 +180,6 @@ t.tBodies[0].appendChild(newRows[i]);

compareNumber: function(a, b) {
var a = parseFloat(a);
a = isNaN(a) ? 0 : a;
var b = parseFloat(b);
b = isNaN(b) ? 0 : b;
var aa = parseFloat(a);
a = isNaN(aa) ? 0 : aa;
var bb = parseFloat(b);
b = isNaN(bb) ? 0 : bb;
return a - b;

@@ -189,3 +191,3 @@ },

cleanNumber: function(i) {
return i.replace(/[^-?0-9.]/g, '');
return i.replace(/[^\-?0-9.]/g, '');
},

@@ -199,3 +201,3 @@ hasClass: function(el, c) {

object['e' + event + method] = method;
object[event + method] = function(){object['e' + event + method](window.event);}
object[event + method] = function(){object['e' + event + method](window.event);};
object.attachEvent('on' + event, object[event + method]);

@@ -202,0 +204,0 @@ } else {

// tablesort.js
// tristen @fallsemo
function Tablesort(a,b){a.tagName==="TABLE"?this.init(a,b||{}):console.error("Element must be a table")}Tablesort.prototype={init:function(a,b){var c=this,d;this.thead=!1,this.options=b,this.options.d=b.descending||!1,a.rows&&a.rows.length>0&&(a.tHead&&a.tHead.rows.length>0?(d=a.tHead.rows[a.tHead.rows.length-1],c.thead=!0):d=a.rows[0]);if(!d)return;for(var e=0;e<d.cells.length;e++){var f=d.cells[e];f.className+=" sort-header",this.addEvent(f,"click",function(a){var b=c.getParent(f,"tr").getElementsByTagName("th");for(var d=0;d<b.length;d++)(c.hasClass(b[d],"sort-up")||c.hasClass(b[d],"sort-down"))&&b[d]!==this&&(b[d].className=b[d].className.replace(" sort-down","").replace(" sort-up",""));c.sortTable(this)})}},sortTable:function(a){var b=this,c=a.cellIndex,d=this.getParent(a,"table"),e=function(a,c){var d=b.getInnerText(a.cells[b.col]).toLowerCase(),e=b.getInnerText(c.cells[b.col]).toLowerCase();return d===e?0:b.options.d?d<e?-1:1:d<e?1:-1},f=function(a,c){var d=b.getInnerText(a.cells[b.col]);d=b.cleanNumber(d);var e=b.getInnerText(c.cells[b.col]);return e=b.cleanNumber(e),b.options.d?b.compareNumber(d,e):b.compareNumber(e,d)};if(d.rows.length<=1)return;var g="",h=0;while(g===""&&h<d.tBodies[0].rows.length){var g=b.getInnerText(d.tBodies[0].rows[h].cells[c]);g=b.trim(g);if(g.substr(0,4)==="<!--"||g.length===0)g="";h++}if(g==="")return;var i;g.match(/^-?[£$€Û¢´]\d/)||g.match(/^-?(\d+[,\.]?)+(E[-+][\d]+)?%?$/)?i=f:i=e,this.col=c;var j=[],k=[];for(var l=0;l<d.tBodies.length;l++)for(var h=0;h<d.tBodies[l].rows[0].length;h++)j[h]=d.tBodies[l].rows[0][h];for(var l=0;l<d.tBodies.length;l++)if(!b.thead)for(var m=1;m<d.tBodies[l].rows.length;m++)k[m-1]=d.tBodies[l].rows[m];else for(var m=0;m<d.tBodies[l].rows.length;m++)k[m]=d.tBodies[l].rows[m];k.sort(i),b.options.d?b.hasClass(a,"sort-up")?(a.className=a.className.replace(/ sort-up/,""),a.className+=" sort-down"):(a.className=a.className.replace(/ sort-down/,""),a.className+=" sort-up",k.reverse()):b.hasClass(a,"sort-down")?(a.className=a.className.replace(/ sort-down/,""),a.className+=" sort-up"):(a.className=a.className.replace(/ sort-up/,""),a.className+=" sort-down",k.reverse());for(var h=0;h<k.length;h++)k[h].className||d.tBodies[0].appendChild(k[h])},getInnerText:function(a){var b=this;if(typeof a=="string"||typeof a=="undefined")return a;if(a.innerText)return a.innerText;var c="",d=a.childNodes,e=d.length;for(var f=0;f<e;f++)switch(d[f].nodeType){case 1:c+=b.getInnerText(d[f]);break;case 3:c+=d[f].nodeValue}return c},getParent:function(a,b){return a===null?null:a.nodeType===1&&a.tagName.toLowerCase()===b.toLowerCase()?a:this.getParent(a.parentNode,b)},compareNumber:function(a,b){var a=parseFloat(a);a=isNaN(a)?0:a;var b=parseFloat(b);return b=isNaN(b)?0:b,a-b},trim:function(a){return a.replace(/^\s+|\s+$/g,"")},cleanNumber:function(a){return a.replace(/[^-?0-9.]/g,"")},hasClass:function(a,b){return(" "+a.className+" ").indexOf(" "+b+" ")>-1},addEvent:function(a,b,c){a.attachEvent?(a["e"+b+c]=c,a[b+c]=function(){a["e"+b+c](window.event)},a.attachEvent("on"+b,a[b+c])):a.addEventListener(b,c,!1)}};
function Tablesort(a,b){a.tagName==="TABLE"?this.init(a,b||{}):console.error("Element must be a table")}Tablesort.prototype={init:function(a,b){var c=this,d;this.thead=!1,this.options=b,this.options.d=b.descending||!1,a.rows&&a.rows.length>0&&(a.tHead&&a.tHead.rows.length>0?(d=a.tHead.rows[a.tHead.rows.length-1],c.thead=!0):d=a.rows[0]);if(!d)return;for(var e=0;e<d.cells.length;e++){var f=d.cells[e];c.hasClass(f,"no-sort")||(f.className+=" sort-header",this.addEvent(f,"click",function(a){var b=c.getParent(f,"tr").getElementsByTagName("th");for(var d=0;d<b.length;d++)(c.hasClass(b[d],"sort-up")||c.hasClass(b[d],"sort-down"))&&b[d]!==this&&(b[d].className=b[d].className.replace(" sort-down","").replace(" sort-up",""));c.sortTable(this)}))}},sortTable:function(a){var b=this,c=a.cellIndex,d=this.getParent(a,"table"),e=function(a,c){var d=b.getInnerText(a.cells[b.col]).toLowerCase(),e=b.getInnerText(c.cells[b.col]).toLowerCase();return d===e?0:b.options.d?d<e?-1:1:d<e?1:-1},f=function(a,c){var d=b.getInnerText(a.cells[b.col]);d=b.cleanNumber(d);var e=b.getInnerText(c.cells[b.col]);return e=b.cleanNumber(e),b.options.d?b.compareNumber(d,e):b.compareNumber(e,d)};if(d.rows.length<=1)return;var g="",h=0;while(g===""&&h<d.tBodies[0].rows.length){var g=b.getInnerText(d.tBodies[0].rows[h].cells[c]);g=b.trim(g);if(g.substr(0,4)==="<!--"||g.length===0)g="";h++}if(g==="")return;var i;g.match(/^-?[£$€Û¢´]\d/)||g.match(/^-?(\d+[,\.]?)+(E[\-+][\d]+)?%?$/)?i=f:i=e,this.col=c;var j=[],k=[],l,m;for(l=0;l<d.tBodies.length;l++)for(h=0;h<d.tBodies[l].rows[0].length;h++)j[h]=d.tBodies[l].rows[0][h];for(l=0;l<d.tBodies.length;l++)if(!b.thead)for(m=1;m<d.tBodies[l].rows.length;m++)k[m-1]=d.tBodies[l].rows[m];else for(m=0;m<d.tBodies[l].rows.length;m++)k[m]=d.tBodies[l].rows[m];k.sort(i),b.options.d?b.hasClass(a,"sort-up")?(a.className=a.className.replace(/ sort-up/,""),a.className+=" sort-down"):(a.className=a.className.replace(/ sort-down/,""),a.className+=" sort-up",k.reverse()):b.hasClass(a,"sort-down")?(a.className=a.className.replace(/ sort-down/,""),a.className+=" sort-up"):(a.className=a.className.replace(/ sort-up/,""),a.className+=" sort-down",k.reverse());for(h=0;h<k.length;h++)k[h].className||d.tBodies[0].appendChild(k[h])},getInnerText:function(a){var b=this;if(typeof a=="string"||typeof a=="undefined")return a;if(a.innerText)return a.innerText;var c="",d=a.childNodes,e=d.length;for(var f=0;f<e;f++)switch(d[f].nodeType){case 1:c+=b.getInnerText(d[f]);break;case 3:c+=d[f].nodeValue}return c},getParent:function(a,b){return a===null?null:a.nodeType===1&&a.tagName.toLowerCase()===b.toLowerCase()?a:this.getParent(a.parentNode,b)},compareNumber:function(a,b){var c=parseFloat(a);a=isNaN(c)?0:c;var d=parseFloat(b);return b=isNaN(d)?0:d,a-b},trim:function(a){return a.replace(/^\s+|\s+$/g,"")},cleanNumber:function(a){return a.replace(/[^\-?0-9.]/g,"")},hasClass:function(a,b){return(" "+a.className+" ").indexOf(" "+b+" ")>-1},addEvent:function(a,b,c){a.attachEvent?(a["e"+b+c]=c,a[b+c]=function(){a["e"+b+c](window.event)},a.attachEvent("on"+b,a[b+c])):a.addEventListener(b,c,!1)}};

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc