New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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.3.0 to 1.4.0

2

package.json
{
"name": "tablesort",
"description": "A sorting component for HTML tables",
"version": "1.3.0",
"version": "1.4.0",
"author": "tristen <@fallsemo>",

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

# tablesort.js
Tablesort is a small and simple sorting component for tables written in Javascript. It has no dependencies and should have no interference with other libraries.
Tablesort is a small & simple sorting component for tables written in Javascript. It has no dependencies and should have no interference with other libraries.

@@ -13,12 +13,12 @@ ## Basic usage

```
## Features
## Sort Types
* Sort strings
* Sort numbers
* Sort currency
* Basic date sorting: in `dd/mm/yy` or `dd-mm-yy` format.
* strings
* numbers
* currency
* Basic dates in `dd/mm/yy` or `dd-mm-yy` format. Years can be 4 digits. Days and Months can be 1 or 2 digits.
## Additional options
__1. Ascending/Descending__
__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.

@@ -32,3 +32,3 @@

__2. Exclude columns__
__Exclude columns__
For columns that do not require sorting, you can add a class of `no-sort`

@@ -39,7 +39,21 @@ ``` html

__Refresh sort on appended data__
Tablesort supports sorting when new data has been added. Simply call the refresh method.
``` js
var table = document.getElementById('table-id');
var sort = new Tablesort(table);
// Make some Ajax request to fetch new data and on success:
sort.refresh();
```
[See homepage page for example](http://tristen.ca/tablesort/demo/#refresh)
## Ender support
Add `tablesort` as an internal chain method to your [Ender](http://ender.no.de) compilation.
$ ender add tablesort
``` shell
$ ender add tablesort
```

@@ -67,3 +81,3 @@ Use it:

margin-top:7px;
border-width:4px 4px 0;
border-width:0 4px 4px;
border-style:solid;

@@ -83,3 +97,4 @@ border-color:#404040 transparent;

table th.sort-up:after {
border-width:0 4px 4px;
border-bottom:none;
border-width:4px 4px 0;
}

@@ -100,5 +115,13 @@ ```

* Tests
* Sort on date
* Pass in an options object to:
- Choose which row to begin sorting on.
- EventListener to rebuild the table.
- EventListener to rebuild the table in Ender.
## Licence
MIT
## Bugs?
[Create an issue](https://github.com/tristen/tablesort/issues)

@@ -26,3 +26,3 @@ // tablesort.js

// Assume first row is the header and attach a click handler to eech.
// Assume first row is the header and attach a click handler to eech.
for(var i = 0; i < firstRow.cells.length; i++) {

@@ -33,3 +33,3 @@ var cell = firstRow.cells[i];

addEvent(cell, 'click', function (e) {
// Delete any sort classes on table headers that are not the current one.
// Delete sort classes on headers that are not the current one.
var siblings = getParent(cell, 'tr').getElementsByTagName('th');

@@ -44,2 +44,3 @@ for(var i = 0; i < siblings.length; i++) {

}
that.current = this;
that.sortTable(this);

@@ -50,7 +51,23 @@ });

},
sortTable: function (header) {
sortTable: function (header, update) {
var that = this;
var column = header.cellIndex;
var t = getParent(header, 'table');
if(t.rows.length <= 1) return;
var item = '', i = 0;
while(item === '' && i < t.tBodies[0].rows.length) {
item = getInnerText(t.tBodies[0].rows[i].cells[column]);
item = trim(item);
// Exclude cell values where commented out HTML exists
if(item.substr(0, 4) === '<!--' || item.length === 0) {
item = '';
}
i++;
}
if(item === '') return;
var sortFunction;
// Possible sortFunction scenarios
var sortCaseInsensitive = function (a, b) {

@@ -76,17 +93,2 @@ var aa = getInnerText(a.cells[that.col]).toLowerCase();

// Work out a type for the column
if(t.rows.length <= 1) return;
var item = '';
var i = 0;
while(item === '' && i < t.tBodies[0].rows.length) {
item = getInnerText(t.tBodies[0].rows[i].cells[column]);
item = trim(item);
if(item.substr(0, 4) === '<!--' || item.length === 0) {
item = '';
}
i++;
}
if(item === '') return;
var sortFunction;
// Sort as number if a currency key exists or number

@@ -98,3 +100,2 @@ if(item.match(/^-?[£$€Û¢´]\d/) || item.match(/^-?(\d+[,\.]?)+(E[-+][\d]+)?%?$/)) {

} else {
console.log('string');
sortFunction = sortCaseInsensitive;

@@ -127,23 +128,25 @@ }

// TODO Optimize.
if(that.options.d) {
if(hasClass(header, 'sort-up')) {
header.className = header.className.replace(/ sort-up/, '');
header.className += ' sort-down';
newRows.reverse();
if (!update) {
if(that.options.d) {
if(hasClass(header, 'sort-up')) {
header.className = header.className.replace(/ sort-up/, '');
header.className += ' sort-down';
} else {
header.className = header.className.replace(/ sort-down/, '');
header.className += ' sort-up';
}
} else {
header.className = header.className.replace(/ sort-down/, '');
header.className += ' sort-up';
if(hasClass(header, 'sort-down')) {
header.className = header.className.replace(/ sort-down/, '');
header.className += ' sort-up';
} else {
header.className = header.className.replace(/ sort-up/, '');
header.className += ' sort-down';
}
}
} else {
if(hasClass(header, 'sort-down')) {
header.className = header.className.replace(/ sort-down/, '');
header.className += ' sort-up';
} else {
header.className = header.className.replace(/ sort-up/, '');
header.className += ' sort-down';
newRows.reverse();
}
}
// Before we append should we reverse the new array or not?
if(hasClass(header, 'sort-down')) newRows.reverse();
// append rows that already exist rather than creating new ones

@@ -153,4 +156,7 @@ for(i = 0; i < newRows.length; i++) {

t.tBodies[0].appendChild(newRows[i]);
}
}
}
},
refresh: function() {
if (this.current !== undefined) this.sortTable(this.current, true);
}

@@ -157,0 +163,0 @@ };

// tablesort.js
// tristen @fallsemo
(function(){function a(a,b){a.tagName==="TABLE"?this.init(a,b||{}):console.error("Element must be a table")}a.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];l(f,"no-sort")||(f.className+=" sort-header",m(f,"click",function(a){var b=g(f,"tr").getElementsByTagName("th");for(var d=0;d<b.length;d++)(l(b[d],"sort-up")||l(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=g(a,"table"),m=function(a,c){var d=h(a.cells[b.col]).toLowerCase(),e=h(c.cells[b.col]).toLowerCase();return d===e?0:d<e?1:-1},n=function(a,c){var d=h(a.cells[b.col]);d=k(d);var e=h(c.cells[b.col]);return e=k(e),i(e,d)},o=function(a,c){var d=h(a.cells[b.col]).toLowerCase(),e=h(c.cells[b.col]).toLowerCase();return f(e)-f(d)};if(d.rows.length<=1)return;var p="",q=0;while(p===""&&q<d.tBodies[0].rows.length){p=h(d.tBodies[0].rows[q].cells[c]),p=j(p);if(p.substr(0,4)==="<!--"||p.length===0)p="";q++}if(p==="")return;var r;p.match(/^-?[£$€Û¢´]\d/)||p.match(/^-?(\d+[,\.]?)+(E[-+][\d]+)?%?$/)?r=n:e(p)?r=o:(console.log("string"),r=m),this.col=c;var s=[],t=[],u,v;for(u=0;u<d.tBodies.length;u++)for(q=0;q<d.tBodies[u].rows[0].length;q++)s[q]=d.tBodies[u].rows[0][q];for(u=0;u<d.tBodies.length;u++)if(!b.thead)for(v=1;v<d.tBodies[u].rows.length;v++)t[v-1]=d.tBodies[u].rows[v];else for(v=0;v<d.tBodies[u].rows.length;v++)t[v]=d.tBodies[u].rows[v];t.sort(r),b.options.d?l(a,"sort-up")?(a.className=a.className.replace(/ sort-up/,""),a.className+=" sort-down",t.reverse()):(a.className=a.className.replace(/ sort-down/,""),a.className+=" sort-up"):l(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",t.reverse());for(q=0;q<t.length;q++)t[q].className||d.tBodies[0].appendChild(t[q])}};var b=/(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\.?\,?\s*/i,c=/\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}/,d=/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/i,e=function(a){return(a.search(b)!==-1||a.search(c)!==-1||a.search(d!==-1))!==-1},f=function(a){return a=a.replace(/\-/g,"/"),a=a.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{2})/,"$1/$2/$3"),(new Date(a)).getTime()},g=function(a,b){return a===null?null:a.nodeType===1&&a.tagName.toLowerCase()===b.toLowerCase()?a:g(a.parentNode,b)},h=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},i=function(a,b){var c=parseFloat(a);a=isNaN(c)?0:c;var d=parseFloat(b);return b=isNaN(d)?0:d,a-b},j=function(a){return a.replace(/^\s+|\s+$/g,"")},k=function(a){return a.replace(/[^\-?0-9.]/g,"")},l=function(a,b){return(" "+a.className+" ").indexOf(" "+b+" ")>-1},m=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)};window.Tablesort=a})();
(function(){function a(a,b){a.tagName==="TABLE"?this.init(a,b||{}):console.error("Element must be a table")}a.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];l(f,"no-sort")||(f.className+=" sort-header",m(f,"click",function(a){var b=g(f,"tr").getElementsByTagName("th");for(var d=0;d<b.length;d++)(l(b[d],"sort-up")||l(b[d],"sort-down"))&&b[d]!==this&&(b[d].className=b[d].className.replace(" sort-down","").replace(" sort-up",""));c.current=this,c.sortTable(this)}))}},sortTable:function(a,b){var c=this,d=a.cellIndex,m=g(a,"table");if(m.rows.length<=1)return;var n="",o=0;while(n===""&&o<m.tBodies[0].rows.length){n=h(m.tBodies[0].rows[o].cells[d]),n=j(n);if(n.substr(0,4)==="<!--"||n.length===0)n="";o++}if(n==="")return;var p,q=function(a,b){var d=h(a.cells[c.col]).toLowerCase(),e=h(b.cells[c.col]).toLowerCase();return d===e?0:d<e?1:-1},r=function(a,b){var d=h(a.cells[c.col]);d=k(d);var e=h(b.cells[c.col]);return e=k(e),i(e,d)},s=function(a,b){var d=h(a.cells[c.col]).toLowerCase(),e=h(b.cells[c.col]).toLowerCase();return f(e)-f(d)};n.match(/^-?[£$€Û¢´]\d/)||n.match(/^-?(\d+[,\.]?)+(E[-+][\d]+)?%?$/)?p=r:e(n)?p=s:p=q,this.col=d;var t=[],u=[],v,w;for(v=0;v<m.tBodies.length;v++)for(o=0;o<m.tBodies[v].rows[0].length;o++)t[o]=m.tBodies[v].rows[0][o];for(v=0;v<m.tBodies.length;v++)if(!c.thead)for(w=1;w<m.tBodies[v].rows.length;w++)u[w-1]=m.tBodies[v].rows[w];else for(w=0;w<m.tBodies[v].rows.length;w++)u[w]=m.tBodies[v].rows[w];u.sort(p),b||(c.options.d?l(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"):l(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")),l(a,"sort-down")&&u.reverse();for(o=0;o<u.length;o++)u[o].className||m.tBodies[0].appendChild(u[o])},refresh:function(){this.current!==undefined&&this.sortTable(this.current,!0)}};var b=/(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\.?\,?\s*/i,c=/\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}/,d=/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/i,e=function(a){return(a.search(b)!==-1||a.search(c)!==-1||a.search(d!==-1))!==-1},f=function(a){return a=a.replace(/\-/g,"/"),a=a.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{2})/,"$1/$2/$3"),(new Date(a)).getTime()},g=function(a,b){return a===null?null:a.nodeType===1&&a.tagName.toLowerCase()===b.toLowerCase()?a:g(a.parentNode,b)},h=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},i=function(a,b){var c=parseFloat(a);a=isNaN(c)?0:c;var d=parseFloat(b);return b=isNaN(d)?0:d,a-b},j=function(a){return a.replace(/^\s+|\s+$/g,"")},k=function(a){return a.replace(/[^\-?0-9.]/g,"")},l=function(a,b){return(" "+a.className+" ").indexOf(" "+b+" ")>-1},m=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)};window.Tablesort=a})();

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