File: test.js

package info (click to toggle)
node-array-flatten 2.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 208 kB
  • sloc: makefile: 2
file content (60 lines) | stat: -rw-r--r-- 1,565 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* global describe, it */

var assert = require('assert')
var flatten = require('./')

describe('array-flatten', function () {
  describe('flatten', function () {
    it('should flatten an array', function () {
      var result = flatten([1, [2, [3, [4, [5]]], 6, [[7], 8], 9], 10])

      assert.deepEqual(result, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
    })

    it('should throw on non-array', function () {
      assert.throws(function () {
        flatten('test')
      }, TypeError)
    })

    it('should work with non-array', function () {
      var result = flatten.from('test')

      assert.deepEqual(result, ['t', 'e', 's', 't'])
    })
  })

  describe('depth', function () {
    it('should flatten an array to a specific depth', function () {
      var result = flatten.depth([1, [2, [3], 4], 5], 1)

      assert.deepEqual(result, [1, 2, [3], 4, 5])
    })

    it('should clone an array when no depth is specified', function () {
      var array = [1, [2, 3]]
      var clone = flatten.depth(array, 0)

      assert.ok(clone !== array)
      assert.deepEqual(clone, array)
    })

    it('should throw on non-array', function () {
      assert.throws(function () {
        flatten.depth('test', 10)
      }, TypeError)
    })

    it('should throw on non-numeric depth', function () {
      assert.throws(function () {
        flatten.fromDepth('test', 'test')
      }, TypeError)
    })

    it('should work with "from"', function () {
      var result = flatten.fromDepth('test', 1)

      assert.deepEqual(result, ['t', 'e', 's', 't'])
    })
  })
})