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

array-order

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-order - npm Package Compare versions

Comparing version 0.8.0 to 0.9.0

index.js

9

CHANGELOG.md

@@ -11,10 +11,11 @@ <!-- markdownlint-disable MD022 MD024 MD026 MD032 MD041 -->

## [0.8.0] - 2020-04-25
## [0.9.0] - 2020-04-26
### Changed
- rank finding now called `rank` with the arguments reversed to `(source, target)`
### Added
- inplace sorting of arrays based on given raking
- heap permutations
- more tests with permutations
### Changed
- simplified sort
## [0.7.0] - 2017-03-19

@@ -21,0 +22,0 @@ ### Changed

@@ -12,5 +12,7 @@ import shift from './shift.js'

var n = a.length,
c = new Uint16Array(n),
i = 0
c = new Uint16Array(n), //to avoid setting all to 0...
i= 0
while (i<n) c[i++] = 0
return function() {
i = 0
while (c[i] >= i) {

@@ -24,3 +26,2 @@ c[i] = 0

swap(a, 0, n-1)
i=0
return a //same as the initial sequence

@@ -31,5 +32,4 @@ }

++c[i]
i = 0
return a
}
}
{
"name": "array-order",
"version": "0.8.0",
"version": "0.9.0",
"type": "module",
"exports": "./index.js",
"description": "sort an array in-place to a preset order",

@@ -14,3 +15,2 @@ "keywords": [

"author": "Hugo Villeneuve",
"main": "./sort.js",
"devDependencies": {

@@ -20,3 +20,3 @@ "@types/node": "^13.13.2"

"scripts": {
"test": "node ./tst/test-heap && node ./tst/test-sort"
"test": "node ./tst/heap && node ./tst/rank && node ./tst/sort"
},

@@ -23,0 +23,0 @@ "repository": {

<!-- markdownlint-disable MD004 MD007 MD010 MD012 MD041 MD022 MD024 MD032 MD036 -->
# array-order
*sort an array in-place to a preset order. All in place, no intermediate structures created*
*in-place sorting of arrays*
• [Example](#example) • [Features](#features) • [Limitations](#limitations) • [Why](#why) • [API](#api) • [License](#license)
## summary
* **sort** creates a sorter to sort multiple columns `(ref:Array [,compare]) => (a:Array) => a`
* **rank** performs reordering based on given ranks `(ranks:Array, a:Array) => a:Array`
* **heap** creates a function that performs permutation, each one different `a:Array => () => a`
## example
```javascript
var arrayOrder = require('array-order'),
order = [2,1,0,3]
import sort from 'array-order/sort.js'
arrayOrder([2,1,0,3], order) // [0,1,2,3]
arrayOrder(['c','b','a','d'], order) // ['a','b','c','d']
const columns = [[2,1,0,3], ['c','b','a','d']]
columns.forEach(sort(columns[0]))
//each column now sorted according to the first one
//columns == [ [0,1,2,3], ['a','b','c','d'] ]
```
## other exported utils
## exports
* named: `import {sort} from 'array-order'`
* default: `import rank from 'array-order/rank.js'`
the following are needed for testing and are also available
## sort order
the **sort** factory can take an additional compare function to change the sort order: `(ref:Array [,compare]) => (a:Array) => a`
* **undefined** increasing numeric order
* **any negative number** decreasing numeric order
* **a normal compare function** just like Array.prototype.sort: `(a, b) => a - b`
* `array-order/heap.js` creates a function that generates every permutations `(a:Array) => () => a:Array`
* `array-order/swap.js` in place swap of 2 items `(a:Array, i:number, j:number) => a:Array`
* `array-order/shift.js` in place left-swap from i to j `(a:Array, i:number, j:number) => a:Array`
## other utilities
the following other in-place utilities are needed internally and are also available
* `array-order/swap.js` in place swap of 2 items `(a:Array, i:number, j:number) => a`
* `array-order/shift.js` in place left-swap from i to j `(a:Array, i:number, j:number) => a`

@@ -25,0 +40,0 @@ ## License

@@ -1,13 +0,13 @@

export default function(arr, seq) {
var len = seq.length
for (var i=0; i<len; ++i) {
var j = seq[i]
while (j<i) j = seq[j]
if (i!==j) {
var t = arr[i]
arr[i] = arr[j]
arr[j] = t
}
}
return arr
import rank from './rank.js'
export default function(ref, dir) {
var seq = [],
i = 0
while (i<ref.length) seq[i] = i++
seq.sort(
dir < 0 ? function(a,b) { return ref[b]-ref[a] }
: typeof dir === 'function' ? function(a,b) { return dir(ref[a], ref[b]) }
: function(a,b) { return ref[a]-ref[b] }
)
return rank.bind(null,seq)
}

@@ -1,2 +0,2 @@

import sequence from './sequence.js'
import sequence from '../sequence.js'
import heap from '../heap.js'

@@ -18,14 +18,5 @@

sequence(seq) //reset
algo(seq, ref) //seq reordered to match ref
same(seq, ref)
algo(ref, seq) //seq reordered to match ref
same(ref, seq)
}
}
//order(mix, seq) => mix (unchanged)
//order(seq, mix) => mix (mutated to match mix)
//order(mix, mix) => xim
//order(xim, mix) => seq
//order(xim, xim) => mix
//order(mix, xim) => seq

@@ -1,2 +0,2 @@

import sequence from './sequence.js'
import sequence from '../sequence.js'

@@ -3,0 +3,0 @@ // testing utils

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