Skip to content

Commit

Permalink
convert to array in sort()
Browse files Browse the repository at this point in the history
  • Loading branch information
tmhglnd committed Nov 15, 2021
1 parent 6729ba2 commit 684a08e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
2 changes: 2 additions & 0 deletions docs/transform-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,11 @@ Using a second array for repeat times iterates over that array
Mod.repeat([0, 5, 7], 3);
//=> [ 0, 0, 0, 5, 5, 5, 7, 7, 7 ]

// us an array for repetitions per index
Mod.repeat(['c4', 'e4', 'f4', 'g4'], [1, 4, 2, 0]);
//=> [ 'c4', 'e4', 'e4', 'e4', 'e4', 'f4', 'f4' ]

// works with multidimensional arrays
Mod.repeat([[0, 5], [7, 9, 12]], [2, 3]);
//=> [
// [ 0, 5 ],
Expand Down
2 changes: 1 addition & 1 deletion src/statistic.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const Util = require('./utility');
// @return {Array} -> sorted array, object includes order-indeces
//
function sort(a=[0], d=1){
if (!Array.isArray(a)) { return a; }
a = Array.isArray(a)? a : [a];
let arr;
if (a.map(x => typeof x).includes('string')){
arr = a.slice().sort();
Expand Down
19 changes: 11 additions & 8 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ exports.zip = lace;
// @param {Array} -> Array with values returned from lookup
// @return {Array} -> Looked up values
//
function lookup(idx=1, arr=[0]){
function lookup(idx=0, arr=[0]){
idx = (Array.isArray(idx)) ? idx : [idx];
arr = (Array.isArray(arr)) ? arr : [arr];
let a = [];
Expand All @@ -254,20 +254,21 @@ exports.lookup = lookup;

// merge all values of two arrays on the same index
// into a 2D array. preserves length of longest list
// flattens multidimensional arrays to 2 dimensions on merge
//
// @params {Array0, Array1, ..., Array-n} -> Arrays to merge
// @return {Array}
//
function merge(...args){
if (!args.length){ return [0]; }
var l = 0;
let l = 0;
for (let i in args){
l = Math.max(args[i].length, l);
}
var arr = [];
for (var i=0; i<l; i++){
var a = [];
for (var k in args){
let arr = [];
for (let i=0; i<l; i++){
let a = [];
for (let k in args){
let v = args[k][i];
if (v != undefined){
if (Array.isArray(v)) a.push(...v);
Expand All @@ -287,8 +288,10 @@ exports.merge = merge;
// @param {Bool} -> no-double flag (optional, default=false)
// @return {Array}
//
function palindrome(arr=[0], noDouble=false){
var rev = arr.slice().reverse();
function palindrome(arr, noDouble=false){
if (arr === undefined){ return [0] };

let rev = arr.slice().reverse();
if (noDouble){
rev = rev.slice(1, rev.length-1);
}
Expand Down
10 changes: 7 additions & 3 deletions test/serialism.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,17 +347,19 @@ function testMod(){
test("Mod.invert([-1, 2, [[7, 9], 14]])");

// test("Mod.lace([0, 2, 4], [1, 3, 5], ['hello'])");
test("Mod.lace()");
test("Mod.lace([0, 0, 0], [7, 7], [9, 9, 9, 9])");
test("Mod.lace([0, [0, 0]], [[7,7]], [9, [[9, 9], 9], 9])");
test("Mod.lace(['c', 'c', 'c', 'c'], ['g', 'g'], ['e'])");

// test("Mod.lookup()");
test("Mod.lookup()");
test("Mod.lookup([0, 1, 1, 2, 0, 2, 2, 1], ['c4', 'e4', 'f4', 'g4'])");
test("Mod.lookup([0, [1, 1, [2, 3 ], 0], 2], ['c4', 'e4', 'f4', 'g4'])");
test("Mod.lookup([-2, 5, 7, 12], ['c4', 'e4', 'f4', 'g4'])");
test("Mod.lookup(Gen.cosine(16, 5.32, 0, 12), [0, 0, 2, 3, 3, 5, 7, 7, 8, 8, 11, 11])");
test("Mod.lookup([0, 'foo', ['1', 'bar']], [1, 2, 3])")
/*

// var merArr1 = [0, 3, 7];
// var merArr2 = [3, 12];
// var merArr3 = [12, -1, 19, 5];
Expand All @@ -378,11 +380,10 @@ function testMod(){
test("Mod.palindrome(['c4', 'f4', 'g4'], true)");
// test("Mod.palindrome([0, 1, 2, 3], 1)");

test("Mod.repeat([0, 5, 7], 3)");
test("Mod.repeat(['c4', 'e4', 'f4', 'g4'], [1, 4, 2, 0])");
// test("Mod.repeat(['kick', 'hat'], [1, 4])");
test("Mod.repeat([[0, 5], [7, 9, 12]], [2, 3])");
test("Mod.repeat([[0, 5], [7, [9, 12]]], [2, 3])");

// var revArr = [0, 5, 7, 12];
// console.log(Mod.reverse(revArr));
Expand All @@ -402,6 +403,9 @@ function testMod(){
test("Mod.sort(['e4', 'g3', 'c4', 'f3', 'b5'])");
test("Mod.sort([-5, [7, 0, 3], 12, -7, 9], -1)");

test("Mod.sort([-1, [3, 5, -2], 5, 10])");
test("Mod.sort(10)");
/*
// var sprArr1 = [12, 19, 24];
// var sprArr2 = [1, 0, 0, 1, 1, 0, 1, 0, 0.2];
// console.log(Mod.spray(sprArr1, sprArr2));
Expand Down

0 comments on commit 684a08e

Please sign in to comment.