Comparing version 0.1.1 to 0.1.2
{ | ||
"name": "rambda", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Lightweight alternative to Ramda", | ||
@@ -5,0 +5,0 @@ "main": "rambda.min.js", |
210
rambda.js
@@ -1,11 +0,89 @@ | ||
const R = require("./compose") | ||
const round = (num) =>{ | ||
return Math.round(num * 100) / 100 | ||
const R = require("./ramda") | ||
const add = (a,b)=>{ | ||
if(b === undefined){ | ||
return c => add(a,c) | ||
}else{ | ||
return a + b | ||
} | ||
} | ||
const range = (start, end) => { | ||
let willReturn = [] | ||
for(let i = start; i<end; i++){ | ||
willReturn.push(i); | ||
const adjust = (fn,index,arr)=>{ | ||
if(index === undefined){ | ||
return (indexHolder, arrHolder) => adjust(fn,indexHolder, arrHolder) | ||
}else if(arr === undefined){ | ||
return holder => adjust(fn,index, holder) | ||
}else{ | ||
return arr.map((val,key)=>{ | ||
if(key===index){ | ||
return fn(arr[index]) | ||
}else{ | ||
return val | ||
} | ||
}) | ||
} | ||
} | ||
const any = (fn,arr) =>{ | ||
if(arr===undefined){ | ||
return holder => any(fn,holder) | ||
}else{ | ||
let flag = false | ||
arr.map(val=>{ | ||
if(fn(val)===true){ | ||
flag = true | ||
} | ||
}) | ||
return flag | ||
} | ||
} | ||
const append = (val,arr)=>{ | ||
if(arr === undefined){ | ||
return holder => append(val,holder) | ||
}else{ | ||
const clone = arr | ||
clone.unshift(val) | ||
return clone | ||
} | ||
} | ||
const contains = (val,arr) =>{ | ||
if(arr===undefined){ | ||
return holder => contains(val,holder) | ||
}else{ | ||
return any(value=>val===value,arr) | ||
} | ||
} | ||
const filter = (fn,arr) =>{ | ||
if(arr===undefined){ | ||
return holder => filter(fn,holder) | ||
}else{ | ||
return arr.filter(fn) | ||
} | ||
} | ||
const flatten = arr => { | ||
const willReturn = [] | ||
arr.map(val => { | ||
if (val instanceof Array) { | ||
val.map(value => { | ||
if (value instanceof Array) { | ||
value.map(lastValue => { | ||
if (lastValue instanceof Array) { | ||
willReturn.push(...lastValue) | ||
} else { | ||
willReturn.push(lastValue) | ||
} | ||
}) | ||
} else { | ||
willReturn.push(value) | ||
} | ||
}) | ||
} else { | ||
willReturn.push(val) | ||
} | ||
}) | ||
return willReturn | ||
@@ -15,59 +93,101 @@ } | ||
const drop = (dropNumber, arr) => { | ||
let arrClone = arr | ||
return arrClone.slice(dropNumber) | ||
if(arr === undefined){ | ||
return holder => drop(dropNumber, holder) | ||
}else{ | ||
const arrClone = arr | ||
return arrClone.slice(dropNumber) | ||
} | ||
} | ||
const dropLast = (dropNumber, arr) => { | ||
let arrClone = arr | ||
return arrClone.slice(0,-dropNumber) | ||
if(arr === undefined){ | ||
return holder => dropLast(dropNumber, holder) | ||
}else{ | ||
const arrClone = arr | ||
return arrClone.slice(0, -dropNumber) | ||
} | ||
} | ||
const head = (arr)=>{ | ||
return dropLast(arr.length-1,arr) | ||
const head = arr => dropLast(arr.length - 1, arr) | ||
const init = arr => dropLast(1, arr) | ||
const join = (glue,arr) =>{ | ||
if(arr===undefined){ | ||
return holder => join(glue,holder) | ||
}else{ | ||
return arr.join(glue) | ||
} | ||
} | ||
const init = (arr)=>{ | ||
return dropLast(1,arr) | ||
const map = (fn,arr) =>{ | ||
if(arr===undefined){ | ||
return holder => map(fn,holder) | ||
}else{ | ||
return arr.map(fn) | ||
} | ||
} | ||
const tail = (arr)=>{ | ||
return drop(1,arr) | ||
const last = arr => arr[ arr.length - 1 ] | ||
const prepend = (val,arr)=>{ | ||
if(arr === undefined){ | ||
return holder => prepend(val,holder) | ||
}else{ | ||
const clone = arr | ||
clone.push(val) | ||
return clone | ||
} | ||
} | ||
const last = (arr)=>{ | ||
return arr[arr.length-1] | ||
const range = (start, end) => { | ||
const willReturn = [] | ||
for (let i = start; i < end; i++) { | ||
willReturn.push(i) | ||
} | ||
return willReturn | ||
} | ||
const flatten = (arr)=>{ | ||
const willReturn = [] | ||
arr.map(val=>{ | ||
if(val instanceof Array){ | ||
val.map(value =>{ | ||
if(value instanceof Array){ | ||
value.map(lastValue=>{ | ||
if(lastValue instanceof Array){ | ||
willReturn.push(...lastValue) | ||
}else{ | ||
willReturn.push(lastValue) | ||
} | ||
}) | ||
}else{ | ||
willReturn.push(value) | ||
} | ||
}) | ||
}else{ | ||
willReturn.push(val) | ||
} | ||
}) | ||
return willReturn | ||
const subtract = (a,b)=>{ | ||
if(b === undefined){ | ||
return c => subtract(a,c) | ||
}else{ | ||
return a - b | ||
} | ||
} | ||
module.exports.round = round | ||
const split = (glue,str) =>{ | ||
if(str===undefined){ | ||
return holder => split(glue,holder) | ||
}else{ | ||
return str.split(glue) | ||
} | ||
} | ||
const tail = arr => drop(1, arr) | ||
module.exports.add = add | ||
module.exports.adjust = adjust | ||
module.exports.any = any | ||
module.exports.append = append | ||
module.exports.compose = R.compose | ||
module.exports.contains = contains | ||
module.exports.drop = drop | ||
module.exports.dropLast = dropLast | ||
module.exports.filter = filter | ||
module.exports.flatten = flatten | ||
module.exports.flip = R.flip | ||
module.exports.head = head | ||
module.exports.init = init | ||
module.exports.join = join | ||
module.exports.last = last | ||
module.exports.map = map | ||
module.exports.prepend = prepend | ||
module.exports.range = range | ||
module.exports.split = split | ||
module.exports.subtract = subtract | ||
module.exports.tail = tail | ||
module.exports.last = last | ||
module.exports.flatten = flatten | ||
module.exports.compose = R.compose |
157
test.js
@@ -0,13 +1,51 @@ | ||
const Ramda = require("ramda") | ||
const R = require("./rambda") | ||
describe("common cases", () => { | ||
it("compose", () => { | ||
it("add/adjust",()=>{ | ||
expect( | ||
R.adjust(R.add(1))(1)([1,2,3]) | ||
).toEqual([1,3,3]) | ||
}) | ||
it("any/subtract",()=>{ | ||
expect( | ||
R.compose( | ||
R.tail, | ||
R.init, | ||
R.flatten | ||
)([ [ [ 1, [ 2 ] ] ], [ 3, 4 ] ]) | ||
).toEqual([ 2, 3 ]) | ||
R.any(val=>val<5), | ||
R.map(R.subtract(10)), | ||
R.adjust(R.add(1),0) | ||
)([0,2,3,4,5,6,7,8,9]) | ||
).toBeTruthy() | ||
}) | ||
it("append",()=>{ | ||
expect( | ||
R.compose( | ||
R.flatten, | ||
R.map(R.append(0)) | ||
)([[1],[2],[3]]) | ||
).toEqual( [0, 1, 0, 2, 0, 3]) | ||
}) | ||
it("contains",()=>{ | ||
expect( | ||
R.compose( | ||
R.contains(2), | ||
R.flatten, | ||
R.map(R.append(0)) | ||
)([[1],[2],[3]]) | ||
).toBeTruthy() | ||
}) | ||
it("filter",()=>{ | ||
expect( | ||
R.compose( | ||
R.flatten, | ||
R.filter(val=>val>2), | ||
R.flatten, | ||
)([[1],[2],[3],4]) | ||
).toEqual([3,4]) | ||
}) | ||
it("flatten", () => { | ||
@@ -24,2 +62,109 @@ expect( | ||
}) | ||
it("flip",()=>{ | ||
expect( | ||
R.compose( | ||
R.map(R.flip(R.subtract)(10)), | ||
R.adjust(R.add(1),0) | ||
)([0,2,3,4,5,6,7,8,9]) | ||
).toEqual([-9, -8, -7, -6, -5, -4, -3, -2, -1]) | ||
}) | ||
it("drop",()=>{ | ||
expect( | ||
R.compose( | ||
R.drop(2), | ||
R.flatten, | ||
R.filter(val=>val>1), | ||
R.flatten, | ||
)([[1],[2],[3],4]) | ||
).toEqual([4]) | ||
}) | ||
it("dropLast",()=>{ | ||
expect( | ||
R.compose( | ||
R.dropLast(2), | ||
R.flatten, | ||
R.filter(val=>val>1), | ||
R.flatten, | ||
)([[1],[2],[3],4]) | ||
).toEqual([2]) | ||
}) | ||
it("head",()=>{ | ||
expect( | ||
R.compose( | ||
R.head, | ||
R.flatten, | ||
R.filter(val=>val>1), | ||
R.flatten, | ||
)([[1],[2],[3],4]) | ||
).toEqual([2]) | ||
}) | ||
it("init/tail", () => { | ||
expect( | ||
R.compose( | ||
R.tail, | ||
R.init, | ||
R.flatten | ||
)([ [ [ 1, [ 2 ] ] ], [ 3, 4 ] ]) | ||
).toEqual([ 2, 3 ]) | ||
}) | ||
it("join",()=>{ | ||
expect( | ||
R.join( | ||
"|" | ||
)(["foo","bar","baz"]) | ||
).toEqual("foo|bar|baz") | ||
}) | ||
it("map",()=>{ | ||
expect( | ||
R.compose( | ||
val => val.reverse(), | ||
R.map(R.subtract(10)), | ||
R.adjust(R.add(1),0) | ||
)([0,2,3,4,5,6,7,8,9]) | ||
).toEqual([1,2,3,4,5,6,7,8,9]) | ||
}) | ||
it("last",()=>{ | ||
expect( | ||
R.compose( | ||
R.last, | ||
R.map(R.last) | ||
)(["foo","bar","baz"]) | ||
).toEqual("z") | ||
}) | ||
it("prepend",()=>{ | ||
expect( | ||
R.compose( | ||
R.flatten, | ||
R.map(R.prepend(0)) | ||
)([[1],[2],[3]]) | ||
).toEqual( [1, 0, 2, 0, 3,0]) | ||
}) | ||
it("range",()=>{ | ||
expect( | ||
R.range( | ||
0, | ||
10 | ||
) | ||
).toEqual([0,1,2,3,4,5,6,7,8,9]) | ||
}) | ||
it("split",()=>{ | ||
expect( | ||
R.split( | ||
"|" | ||
)("foo|bar|baz") | ||
).toEqual(["foo","bar","baz"]) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
21758
10
316