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 f904619

Browse filesBrowse files
committed
Improvements to Record constructor. Added custom get/has to Array and Object Sequences. Improved Array and Object sequences. Test more of Record in custom Conversion
1 parent 55d6c5f commit f904619
Copy full SHA for f904619

File tree

Expand file treeCollapse file tree

7 files changed

+243
-139
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+243
-139
lines changed

‎__tests__/Conversion.ts

Copy file name to clipboardExpand all lines: __tests__/Conversion.ts
+23-1Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('Conversion', () => {
4545
list: [1, 2, 3]
4646
};
4747

48-
var Point = Immutable.Record({x:0, y:0});
48+
var Point = Immutable.Record({x:0, y:0}, 'Point');
4949

5050
var immutableData = Map({
5151
deepList: Vector(
@@ -89,6 +89,27 @@ describe('Conversion', () => {
8989
list: Vector(1, 2, 3)
9090
});
9191

92+
var immutableOrderedDataString = 'OrderedMap { ' +
93+
'deepList: Vector [ '+
94+
'OrderedMap { '+
95+
'position: "first"'+
96+
' }, ' +
97+
'OrderedMap { '+
98+
'position: "second"'+
99+
' }, '+
100+
'OrderedMap { '+
101+
'position: "third"'+
102+
' }' +
103+
' ], '+
104+
'deepMap: OrderedMap { '+
105+
'a: "A", '+
106+
'b: "B"'+
107+
' }, '+
108+
'point: Point { x: 10, y: 20 }, '+
109+
'string: "Hello", '+
110+
'list: Vector [ 1, 2, 3 ]'+
111+
' }';
112+
92113
it('Converts deep JS to deep immutable sequences', () => {
93114
expect(Immutable.fromJSON(js)).is(immutableData);
94115
});
@@ -101,6 +122,7 @@ describe('Conversion', () => {
101122
return Array.isArray(this[key]) ? sequence.toVector() : sequence.toOrderedMap();
102123
});
103124
expect(seq).is(immutableOrderedData);
125+
expect(seq.toString()).is(immutableOrderedDataString);
104126
});
105127

106128
it('Converts deep sequences to JSON', () => {

‎dist/Immutable.d.ts

Copy file name to clipboardExpand all lines: dist/Immutable.d.ts
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,8 @@ export declare function OrderedMap<V>(array: Array<V>): Map<number, V>;
993993
* myRecord.getAB() // 4
994994
*
995995
*/
996-
export declare function Record(defaultValues: Object): RecordClass;
996+
export declare function Record(defaultValues: Sequence<string, any>, name?: string): RecordClass;
997+
export declare function Record(defaultValues: {[key: string]: any}, name?: string): RecordClass;
997998

998999
export interface RecordClass {
9991000
new (): Map<string, any>;

‎dist/Record.js

Copy file name to clipboardExpand all lines: dist/Record.js
+27-21Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,29 @@ for(var Sequence____Key in Sequence){if(Sequence.hasOwnProperty(Sequence____Key)
88
var RecordType = function(values) {
99
this.$Record_map = ImmutableMap(values);
1010
};
11+
defaultValues = Sequence(defaultValues);
1112
RecordType.prototype = Object.create(Record.prototype);
1213
RecordType.prototype.constructor = RecordType;
1314
RecordType.prototype.$Record_name = name;
1415
RecordType.prototype.$Record_defaultValues = defaultValues;
1516

1617
var keys = Object.keys(defaultValues);
1718
RecordType.prototype.length = keys.length;
18-
keys.forEach(function(key) {
19-
Object.defineProperty(RecordType.prototype, key, {
20-
get: function() {
21-
return this.get(key);
22-
},
23-
set: function(value) {
24-
if (!this.__ownerID) {
25-
throw new Error('Cannot set on an immutable record.');
19+
if (Object.defineProperty) {
20+
defaultValues.forEach(function(_, key) {
21+
Object.defineProperty(RecordType.prototype, key, {
22+
get: function() {
23+
return this.get(key);
24+
},
25+
set: function(value) {
26+
if (!this.__ownerID) {
27+
throw new Error('Cannot set on an immutable record.');
28+
}
29+
this.set(key, value);
2630
}
27-
this.set(key, value);
28-
}
29-
});
30-
}.bind(this));
31+
});
32+
}.bind(this));
33+
}
3134

3235
return RecordType;
3336
}
@@ -39,14 +42,14 @@ for(var Sequence____Key in Sequence){if(Sequence.hasOwnProperty(Sequence____Key)
3942
// @pragma Access
4043

4144
Record.prototype.has=function(k) {"use strict";
42-
return this.$Record_defaultValues.hasOwnProperty(k);
45+
return this.$Record_defaultValues.has(k);
4346
};
4447

4548
Record.prototype.get=function(k, undefinedValue) {"use strict";
4649
if (undefinedValue !== undefined && !this.has(k)) {
4750
return undefinedValue;
4851
}
49-
return this.$Record_map.get(k, this.$Record_defaultValues[k]);
52+
return this.$Record_map.get(k, this.$Record_defaultValues.get(k));
5053
};
5154

5255
// @pragma Modification
@@ -63,19 +66,22 @@ for(var Sequence____Key in Sequence){if(Sequence.hasOwnProperty(Sequence____Key)
6366
if (k == null || !this.has(k)) {
6467
return this;
6568
}
66-
if (this.__ownerID) {
67-
this.$Record_map.set(k, v);
68-
return this;
69-
}
7069
var newMap = this.$Record_map.set(k, v);
71-
if (newMap === this.$Record_map) {
70+
if (this.__ownerID || newMap === this.$Record_map) {
7271
return this;
7372
}
7473
return this.$Record_make(newMap);
7574
};
7675

7776
Record.prototype.delete=function(k) {"use strict";
78-
return this.set(k, this.$Record_defaultValues[k]);
77+
if (k == null || !this.has(k)) {
78+
return this;
79+
}
80+
var newMap = this.$Record_map.delete(k);
81+
if (this.__ownerID || newMap === this.$Record_map) {
82+
return this;
83+
}
84+
return this.$Record_make(newMap);
7985
};
8086

8187
// @pragma Mutability
@@ -97,7 +103,7 @@ for(var Sequence____Key in Sequence){if(Sequence.hasOwnProperty(Sequence____Key)
97103

98104
Record.prototype.__iterate=function(fn, reverse) {"use strict";
99105
var record = this;
100-
return Sequence(this.$Record_defaultValues).map(function(_, k) {return record.get(k);}).__iterate(fn, reverse);
106+
return this.$Record_defaultValues.map(function(_, k) {return record.get(k);}).__iterate(fn, reverse);
101107
};
102108

103109
Record.prototype.$Record_empty=function() {"use strict";

‎dist/Sequence.js

Copy file name to clipboardExpand all lines: dist/Sequence.js
+82-48Lines changed: 82 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,12 @@ var Immutable = require('./Immutable');
1111
}
1212
if (!Array.isArray(value)) {
1313
if (value && value.constructor === Object) {
14-
var keys = Object.keys(value);
15-
var objectSequence = makeSequence();
16-
objectSequence.length = keys.length;
17-
objectSequence.toObject = function() {return value;};
18-
objectSequence.__iterate = objectIterator.bind(null, value, keys);
19-
return objectSequence;
14+
return new ObjectSequence(value);
2015
}
2116
value = [value];
2217
}
2318
}
24-
var arraySequence = makeIndexedSequence();
25-
arraySequence.length = value.length;
26-
arraySequence.toArray = function() {return value;};
27-
arraySequence.__iterate = arrayIterator.bind(null, value);
28-
return arraySequence;
19+
return new ArraySequence(value);
2920
}
3021

3122
Sequence.prototype.toString=function() {"use strict";
@@ -40,7 +31,7 @@ var Immutable = require('./Immutable');
4031
};
4132

4233
Sequence.prototype.__toStringMapper=function(v, k) {"use strict";
43-
return quoteString(k) + ': ' + quoteString(v);
34+
return k + ': ' + quoteString(v);
4435
};
4536

4637
Sequence.prototype.toJSON=function() {"use strict";
@@ -707,6 +698,85 @@ IndexedSequence.prototype.__toJS = IndexedSequence.prototype.toArray;
707698
IndexedSequence.prototype.__toStringMapper = quoteString;
708699

709700

701+
for(Sequence____Key in Sequence){if(Sequence.hasOwnProperty(Sequence____Key)){ObjectSequence[Sequence____Key]=Sequence[Sequence____Key];}}ObjectSequence.prototype=Object.create(____SuperProtoOfSequence);ObjectSequence.prototype.constructor=ObjectSequence;ObjectSequence.__superConstructor__=Sequence;
702+
function ObjectSequence(object) {"use strict";
703+
var keys = Object.keys(object);
704+
this.$ObjectSequence_object = object;
705+
this.$ObjectSequence_keys = keys;
706+
this.length = keys.length;
707+
}
708+
709+
ObjectSequence.prototype.toObject=function() {"use strict";
710+
return this.$ObjectSequence_object;
711+
};
712+
713+
ObjectSequence.prototype.get=function(key, undefinedValue) {"use strict";
714+
if (undefinedValue !== undefined && !this.has(key)) {
715+
return undefinedValue;
716+
}
717+
return this.$ObjectSequence_object[key];
718+
};
719+
720+
ObjectSequence.prototype.has=function(key) {"use strict";
721+
return this.$ObjectSequence_object.hasOwnProperty(key);
722+
};
723+
724+
ObjectSequence.prototype.__iterate=function(fn, reverse) {"use strict";
725+
var object = this.$ObjectSequence_object;
726+
var keys = this.$ObjectSequence_keys;
727+
var maxIndex = keys.length - 1;
728+
for (var ii = 0; ii <= maxIndex; ii++) {
729+
var iteration = reverse ? maxIndex - ii : ii;
730+
if (fn(object[keys[iteration]], keys[iteration], object) === false) {
731+
break;
732+
}
733+
}
734+
return ii;
735+
};
736+
737+
738+
739+
for(var IndexedSequence____Key in IndexedSequence){if(IndexedSequence.hasOwnProperty(IndexedSequence____Key)){ArraySequence[IndexedSequence____Key]=IndexedSequence[IndexedSequence____Key];}}var ____SuperProtoOfIndexedSequence=IndexedSequence===null?null:IndexedSequence.prototype;ArraySequence.prototype=Object.create(____SuperProtoOfIndexedSequence);ArraySequence.prototype.constructor=ArraySequence;ArraySequence.__superConstructor__=IndexedSequence;
740+
function ArraySequence(array) {"use strict";
741+
this.$ArraySequence_array = array;
742+
this.length = array.length;
743+
}
744+
745+
ArraySequence.prototype.toArray=function() {"use strict";
746+
return this.$ArraySequence_array;
747+
};
748+
749+
ArraySequence.prototype.__iterate=function(fn, reverse, flipIndices) {"use strict";
750+
var array = this.$ArraySequence_array;
751+
var maxIndex = array.length - 1;
752+
var lastIndex = -1;
753+
if (reverse) {
754+
for (var ii = maxIndex; ii >= 0; ii--) {
755+
if (array.hasOwnProperty(ii) &&
756+
fn(array[ii], flipIndices ? ii : maxIndex - ii, array) === false) {
757+
return lastIndex + 1;
758+
}
759+
lastIndex = ii;
760+
}
761+
return array.length;
762+
} else {
763+
var didFinish = array.every(function(value, index) {
764+
if (fn(value, flipIndices ? maxIndex - index : index, array) === false) {
765+
return false;
766+
} else {
767+
lastIndex = index;
768+
return true;
769+
}
770+
});
771+
return didFinish ? array.length : lastIndex + 1;
772+
}
773+
};
774+
775+
776+
ArraySequence.prototype.get = ObjectSequence.prototype.get;
777+
ArraySequence.prototype.has = ObjectSequence.prototype.has;
778+
779+
710780
function makeSequence() {
711781
return Object.create(Sequence.prototype);
712782
}
@@ -717,42 +787,6 @@ function makeIndexedSequence(parent) {
717787
return newSequence;
718788
}
719789

720-
function arrayIterator(array, fn, reverse, flipIndices) {
721-
var maxIndex = array.length - 1;
722-
var lastIndex = -1;
723-
if (reverse) {
724-
for (var ii = maxIndex; ii >= 0; ii--) {
725-
if (array.hasOwnProperty(ii) &&
726-
fn(array[ii], flipIndices ? ii : maxIndex - ii, array) === false) {
727-
return lastIndex + 1;
728-
}
729-
lastIndex = ii;
730-
}
731-
return array.length;
732-
} else {
733-
var didFinish = array.every(function(value, index) {
734-
if (fn(value, flipIndices ? maxIndex - index : index, array) === false) {
735-
return false;
736-
} else {
737-
lastIndex = index;
738-
return true;
739-
}
740-
});
741-
return didFinish ? array.length : lastIndex + 1;
742-
}
743-
}
744-
745-
function objectIterator(object, keys, fn, reverse) {
746-
var maxIndex = keys.length - 1;
747-
for (var ii = 0; ii <= maxIndex; ii++) {
748-
var iteration = reverse ? maxIndex - ii : ii;
749-
if (fn(object[keys[iteration]], keys[iteration], object) === false) {
750-
break;
751-
}
752-
}
753-
return ii;
754-
}
755-
756790
function getInDeepSequence(seq, keyPath, notFoundValue, pathOffset) {
757791
var nested = seq.get ? seq.get(keyPath[pathOffset], __SENTINEL) : __SENTINEL;
758792
if (nested === __SENTINEL) {

0 commit comments

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