Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e56149c

Browse filesBrowse files
committed
Better Sequence constructor
1 parent f904619 commit e56149c
Copy full SHA for e56149c

File tree

Expand file treeCollapse file tree

4 files changed

+57
-33
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+57
-33
lines changed

‎dist/Immutable.d.ts

Copy file name to clipboardExpand all lines: dist/Immutable.d.ts
+15-5Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,27 @@ export declare function fromJSON(
103103
* as the for-in would iterate through the Object itself.
104104
* * An `IndexedSequence` of all arguments is returned.
105105
*
106-
* Note: if a Sequence is created from a JavaScript Array or plain Object, then
107-
* it can still possibly mutated if the underlying Array or Object is ever
108-
* mutated.
106+
* Note: if a Sequence is created from a JavaScript Array or Object, then it can
107+
* still possibly mutated if the underlying Array or Object is ever mutated.
109108
*/
110109
export declare function Sequence<T>(seq: IndexedSequence<T>): IndexedSequence<T>;
111-
export declare function Sequence<K, V>(seq: Sequence<K, V>): Sequence<K, V>;
112110
export declare function Sequence<T>(array: Array<T>): IndexedSequence<T>;
113-
export declare function Sequence<T>(obj: {[key: string]: T}): Sequence<string, T>;
111+
export declare function Sequence<K, V>(seq: Sequence<K, V>): Sequence<K, V>;
112+
export declare function Sequence<V>(obj: {[key: string]: V}): Sequence<string, V>;
114113
export declare function Sequence<T>(...values: T[]): IndexedSequence<T>;
115114
export declare function Sequence(): Sequence<any, any>;
116115

116+
/**
117+
* Like `Immutable.Sequence()`, `Immutable.Sequence.from()` returns a sequence,
118+
* but always expects a single argument.
119+
*/
120+
export declare module Sequence {
121+
function from<T>(seq: IndexedSequence<T>): IndexedSequence<T>;
122+
function from<T>(array: Array<T>): IndexedSequence<T>;
123+
function from<K, V>(seq: Sequence<K, V>): Sequence<K, V>;
124+
function from<V>(obj: {[key: string]: V}): Sequence<string, V>;
125+
}
126+
117127

118128
export interface Sequence<K, V> {
119129

‎dist/Sequence.js

Copy file name to clipboardExpand all lines: dist/Sequence.js
+14-12Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@ var Immutable = require('./Immutable');
33

44

55
function Sequence(value) {"use strict";
6-
if (arguments.length !== 1) {
7-
value = Array.prototype.slice.call(arguments);
8-
} else {
9-
if (value instanceof Sequence) {
10-
return value;
11-
}
12-
if (!Array.isArray(value)) {
13-
if (value && value.constructor === Object) {
14-
return new ObjectSequence(value);
15-
}
16-
value = [value];
6+
return Sequence.from(
7+
arguments.length === 1 ? value : Array.prototype.slice.call(arguments)
8+
);
9+
}
10+
11+
Sequence.from=function(value) {"use strict";
12+
if (value instanceof Sequence) {
13+
return value;
14+
}
15+
if (!Array.isArray(value)) {
16+
if (value && value.constructor === Object) {
17+
return new ObjectSequence(value);
1718
}
19+
value = [value];
1820
}
1921
return new ArraySequence(value);
20-
}
22+
};
2123

2224
Sequence.prototype.toString=function() {"use strict";
2325
return this.__toString('Seq {', '}');

‎src/Sequence.js

Copy file name to clipboardExpand all lines: src/Sequence.js
+13-11Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ var Immutable = require('./Immutable');
33

44
class Sequence {
55
constructor(value) {
6-
if (arguments.length !== 1) {
7-
value = Array.prototype.slice.call(arguments);
8-
} else {
9-
if (value instanceof Sequence) {
10-
return value;
11-
}
12-
if (!Array.isArray(value)) {
13-
if (value && value.constructor === Object) {
14-
return new ObjectSequence(value);
15-
}
16-
value = [value];
6+
return Sequence.from(
7+
arguments.length === 1 ? value : Array.prototype.slice.call(arguments)
8+
);
9+
}
10+
11+
static from(value) {
12+
if (value instanceof Sequence) {
13+
return value;
14+
}
15+
if (!Array.isArray(value)) {
16+
if (value && value.constructor === Object) {
17+
return new ObjectSequence(value);
1718
}
19+
value = [value];
1820
}
1921
return new ArraySequence(value);
2022
}

‎type-definitions/Immutable.d.ts

Copy file name to clipboardExpand all lines: type-definitions/Immutable.d.ts
+15-5Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,27 @@ export declare function fromJSON(
103103
* as the for-in would iterate through the Object itself.
104104
* * An `IndexedSequence` of all arguments is returned.
105105
*
106-
* Note: if a Sequence is created from a JavaScript Array or plain Object, then
107-
* it can still possibly mutated if the underlying Array or Object is ever
108-
* mutated.
106+
* Note: if a Sequence is created from a JavaScript Array or Object, then it can
107+
* still possibly mutated if the underlying Array or Object is ever mutated.
109108
*/
110109
export declare function Sequence<T>(seq: IndexedSequence<T>): IndexedSequence<T>;
111-
export declare function Sequence<K, V>(seq: Sequence<K, V>): Sequence<K, V>;
112110
export declare function Sequence<T>(array: Array<T>): IndexedSequence<T>;
113-
export declare function Sequence<T>(obj: {[key: string]: T}): Sequence<string, T>;
111+
export declare function Sequence<K, V>(seq: Sequence<K, V>): Sequence<K, V>;
112+
export declare function Sequence<V>(obj: {[key: string]: V}): Sequence<string, V>;
114113
export declare function Sequence<T>(...values: T[]): IndexedSequence<T>;
115114
export declare function Sequence(): Sequence<any, any>;
116115

116+
/**
117+
* Like `Immutable.Sequence()`, `Immutable.Sequence.from()` returns a sequence,
118+
* but always expects a single argument.
119+
*/
120+
export declare module Sequence {
121+
function from<T>(seq: IndexedSequence<T>): IndexedSequence<T>;
122+
function from<T>(array: Array<T>): IndexedSequence<T>;
123+
function from<K, V>(seq: Sequence<K, V>): Sequence<K, V>;
124+
function from<V>(obj: {[key: string]: V}): Sequence<string, V>;
125+
}
126+
117127

118128
export interface Sequence<K, V> {
119129

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.