File tree Expand file tree Collapse file tree 3 files changed +52
-64
lines changed Expand file tree Collapse file tree 3 files changed +52
-64
lines changed Original file line number Diff line number Diff line change 22
22
},
23
23
"bugs" : {
24
24
"url" : " https://github.com/Wolfy87/lazy-array/issues"
25
- },
26
- "dependencies" : {
27
- "lodash" : " ^3.9.2"
28
25
}
29
26
}
Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
- var _ = require ( 'lodash' ) ;
4
3
var LazyArray = require ( './LazyArray' ) ;
5
- var seq = require ( './seq' ) ;
6
4
7
5
/**
8
6
* Constructs a lazy array instance.
@@ -13,8 +11,57 @@ var seq = require('./seq');
13
11
function create ( fn ) {
14
12
return new LazyArray ( fn ) ;
15
13
}
14
+ /**
15
+ * Get the first item in an array.
16
+ *
17
+ * @param {*[] } list
18
+ * @return {* } First item.
19
+ */
20
+ function first ( list ) {
21
+ if ( LazyArray . isLazyArray ( list ) ) {
22
+ return list . fn ( ) [ 0 ] ;
23
+ }
24
+ else {
25
+ return Array . isArray ( list ) ? list [ 0 ] : null ;
26
+ }
27
+ }
28
+
29
+ /**
30
+ * Get the tail of the array.
31
+ *
32
+ * @param {*[] } list
33
+ * @return {*[] } Tail.
34
+ */
35
+ function rest ( list ) {
36
+ if ( LazyArray . isLazyArray ( list ) ) {
37
+ return list . fn ( ) [ 1 ] ;
38
+ }
39
+ else {
40
+ return Array . isArray ( list ) ? list . slice ( 1 ) : [ ] ;
41
+ }
42
+ }
43
+
44
+ /**
45
+ * Constructs a new array by prepending the item on the list. When given a
46
+ * LazyArray instance to cons onto, it will return a value/LazyArray pair.
47
+ *
48
+ * @param {* } item
49
+ * @param {*[] } list
50
+ * @return {*[] } Original list with the item at the front.
51
+ */
52
+ function cons ( item , list ) {
53
+ if ( LazyArray . isLazyArray ( list ) ) {
54
+ return [ item , list ] ;
55
+ }
56
+ else {
57
+ return [ item ] . concat ( Array . isArray ( list ) ? list : [ ] ) ;
58
+ }
59
+ }
16
60
17
- module . exports = _ . assign ( {
61
+ module . exports = {
18
62
LazyArray : LazyArray ,
19
- create : create
20
- } , seq ) ;
63
+ create : create ,
64
+ first : first ,
65
+ rest : rest ,
66
+ cons : cons
67
+ } ;
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments