Skip to content

alanrsoares/es2015-showoff

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 

Repository files navigation

ES2015 Showoff

A series of es2015+ examples on IMMUTABLE data manipulation

Arrays

Concatenate Arrays

const concat = (...xss) => xss.reduce((xs, ys) => [...xs, ...ys], [])
concat([1, 2, 3], [4, 5, 6], [7, 8], [9]) //=> [1, 2, 4, 5, 6, 7, 8, 9]

[IMMUTABLE] Splice

const splice = (xs, start, count, ...adds) =>
  [...xs.slice(start, count), ...adds, ...xs.slice(start + count)]
const fishes = ['angel', 'clown', 'mandarin', 'surgeon']
splice(fishes, 2, 0, 'drum', 'bass') //=> ['angel', 'clown', 'drum', 'bass', 'mandarin', 'surgeon']
fishes //=> ['angel', 'clown', 'mandarin', 'surgeon']

Unique values in an array

const unique = xs => [...new Set(xs)]
unique([1, 2, 2, 5, 1, 2, 4, 32]) //=> [1, 2, 5, 4, 32]

Take prop from a list of objects

const pluck = key => xs => xs.map(x => x[key])
const takeIds = pluck('id')
const takeIds2 = xs => xs.map(({ id }) => id) // non-generic, using object destructuring
takeIds([{ id: 12 }, { id: 2 }, { id: 42 }]) //=> [12, 2, 42]

N-Length 'blank' array

const array = n => [...Array(Math.max(0, n))]
array(3) //=> [undefined, undefined, undefined]

Numeric a-to-b range array

const range = (a = 0, b = 0) =>
  (j => array(Math.abs(a - b)).map(((x, i) => j + i)))(Math.min(a, b))
range(3) //=> [0, 1, 2]
range(2, 6) //=> [2, 3, 4, 5]

[IMMUTABLE] Sort of a numeric array

const sort = xs => [...xs].sort((a, b) => a - b)
const a = [3, 1, 2]
sort(a) //=> [1, 2, 3]
a //=> [3, 1, 2]

[IMMUTABLE] Remove array index

const remove = index => xs => [...xs.slice(0, index), ...xs.slice(index + 1)]
const a = [3, 1, 2]
remove(0)(a) //=> [1, 2]
a //=> [3, 1, 2]

[IMMUTABLE] Update array index

const set = (index, value) => xs => [...xs.slice(0, index), value, ...xs.slice(index + 1)]
const a = [3, 1, 2]
set(0, 'foo')(a) //=> ['foo', 1, 2]
a //=> [3, 1, 2]

Sum of a numeric array

const sum = xs => xs.reduce((a, b) => a + b, 0)
sum([1, 2, 3]) //=> 6

Average/Mean of a numeric array

const avg = xs => sum(xs) / xs.length
avg([1, 2, 3]) //=> 2

Median of a numeric array

const median = xs =>
  (l => xs.length % 2
    ? sort(xs)[Math.floor(l)]
    : sum(sort(xs).slice(l - 1, l + 1)) / 2)(xs.length / 2)
median([1, 2, 3]) //=> 2
median([1, 2, 3, 4, 5, 6]) //=> 3.5

About

A series of es2015+ examples on data manipulation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published