Class: Hash
Class Method Summary
collapse
Instance Method Summary
collapse
-
#==(other) ⇒ Object
(also: #eql?)
-
#[](key) ⇒ Object
-
#[]=(key, value) ⇒ Object
(also: #store)
-
#assoc(object) ⇒ Object
-
#clear ⇒ Object
-
#clone ⇒ Object
(also: #dup)
-
#default(val = undefined) ⇒ Object
-
#default=(object) ⇒ Object
-
#default_proc ⇒ Object
-
#default_proc=(proc) ⇒ Object
-
#delete(key, &block) ⇒ Object
-
#delete_if(&block) ⇒ Object
-
#each(&block) ⇒ Object
(also: #each_pair)
-
#each_key(&block) ⇒ Object
-
#each_value(&block) ⇒ Object
-
#empty? ⇒ Boolean
-
#fetch(key, defaults = undefined, &block) ⇒ Object
-
#flatten(level = undefined) ⇒ Object
-
#has_key?(key) ⇒ Boolean
(also: #include?, #key?, #member?)
-
#has_value?(value) ⇒ Boolean
(also: #value?)
-
#hash ⇒ Object
-
#index(object) ⇒ Object
(also: #key)
-
#indexes(*keys) ⇒ Object
(also: #indices, #values_at)
-
#initialize(defaults = undefined, &block) ⇒ Hash
constructor
-
#inspect ⇒ Object
(also: #to_s)
-
#invert ⇒ Object
-
#keep_if(&block) ⇒ Object
-
#keys ⇒ Object
-
#length ⇒ Object
(also: #size)
-
#merge(other, &block) ⇒ Object
-
#merge!(other, &block) ⇒ Object
(also: #update)
-
#rassoc(object) ⇒ Object
-
#reject(&block) ⇒ Object
-
#replace(other) ⇒ Object
-
#select(&block) ⇒ Object
-
#select!(&block) ⇒ Object
-
#shift ⇒ Object
-
#to_a ⇒ Object
-
#to_h ⇒ Object
-
#to_hash ⇒ Object
-
#values ⇒ Object
Methods included from Enumerable
#all?, #any?, #chunk, #collect, #collect_concat, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_entry, #each_slice, #each_with_index, #each_with_object, #entries, #find_all, #find_index, #first, #grep, #group_by, #inject, #lazy, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reverse_each, #slice_before, #sort, #sort_by, #take, #take_while, #zip
Constructor Details
#initialize(defaults = undefined, &block) ⇒ Hash
Returns a new instance of Hash
72
73
74
75
76
77
78
|
# File 'opal/opal/corelib/hash.rb', line 72
def initialize(defaults = undefined, &block)
%x{
self.none = (defaults === undefined ? nil : defaults);
self.proc = block;
}
self
end
|
Class Method Details
.[](*argv) ⇒ Object
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
|
# File 'opal/opal/corelib/hash.rb', line 9
def self.[](*argv)
%x{
var hash, i, argc = argv.length;
if (argc === 1) {
hash = #{Opal.coerce_to?(argv[0], Hash, :to_hash)};
if (hash !== nil) {
return #{allocate.merge!(`hash`)};
}
argv = #{Opal.coerce_to?(argv[0], Array, :to_ary)};
if (argv === nil) {
#{raise ArgumentError, 'odd number of arguments for Hash'}
}
argc = argv.length;
hash = #{allocate};
for (i = 0; i < argc; i++) {
if (!argv[i].$$is_array) continue;
switch(argv[i].length) {
case 1:
hash.$store(argv[i][0], nil);
break;
case 2:
hash.$store(argv[i][0], argv[i][1]);
break;
default:
#{raise ArgumentError, "invalid number of elements (#{`argv[i].length`} for 1..2)"}
}
}
return hash;
}
if (argc % 2 !== 0) {
#{raise ArgumentError, 'odd number of arguments for Hash'}
}
hash = #{allocate};
for (i = 0; i < argc; i += 2) {
hash.$store(argv[i], argv[i + 1]);
}
return hash;
}
end
|
.allocate ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'opal/opal/corelib/hash.rb', line 58
def self.allocate
%x{
var hash = new self.$$alloc;
hash.map = {};
hash.smap = {};
hash.keys = [];
hash.none = nil;
hash.proc = nil;
return hash;
}
end
|
Instance Method Details
#==(other) ⇒ Object
Also known as:
eql?
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'opal/opal/corelib/hash.rb', line 80
def ==(other)
%x{
if (self === other) {
return true;
}
if (!other.keys || !other.smap || !other.map) {
return false;
}
if (self.keys.length !== other.keys.length) {
return false;
}
var _map = self.map,
smap = self.smap,
_map2 = other.map,
smap2 = other.smap,
map, map2, key, khash, value, value2;
for (var i = 0, length = self.keys.length; i < length; i++) {
key = self.keys[i];
if (key.$$is_string) {
khash = key;
map = smap;
map2 = smap2;
} else {
khash = key.$hash();
map = _map;
map2 = _map2;
}
value = map[khash];
if (value === undefined) console.log('==', key, self);
value2 = map2[khash];
if (value2 === undefined || #{not(`value` == `value2`)}) {
return false;
}
}
return true;
}
end
|
#[](key) ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'opal/opal/corelib/hash.rb', line 126
def [](key)
%x{
var map, khash;
if (key.$$is_string) {
map = self.smap;
khash = key;
} else {
map = self.map;
khash = key.$hash();
}
if (map === undefined) { console.log(self, '[] --> key:', key, khash, map) }
if (Opal.hasOwnProperty.call(map, khash)) {
return map[khash];
}
var proc = #@proc;
if (proc !== nil) {
return #{ `proc`.call self, key };
}
return #@none;
}
end
|
#[]=(key, value) ⇒ Object
Also known as:
store
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'opal/opal/corelib/hash.rb', line 155
def []=(key, value)
%x{
var map, khash, value;
if (key.$$is_string) {
map = self.smap;
khash = key;
} else {
map = self.map;
khash = key.$hash();
}
if (!Opal.hasOwnProperty.call(map, khash)) {
self.keys.push(key);
}
map[khash] = value;
return value;
}
end
|
#assoc(object) ⇒ Object
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'opal/opal/corelib/hash.rb', line 177
def assoc(object)
%x{
var keys = self.keys,
map, key, khash;
for (var i = 0, length = keys.length; i < length; i++) {
key = keys[i];
if (#{`key` == object}) {
if (key.$$is_string) {
map = self.smap;
khash = key;
} else {
map = self.map;
khash = key.$hash();
}
return [key, map[khash]];
}
}
return nil;
}
end
|
#clear ⇒ Object
202
203
204
205
206
207
208
209
|
# File 'opal/opal/corelib/hash.rb', line 202
def clear
%x{
self.map = {};
self.smap = {};
self.keys = [];
return self;
}
end
|
#clone ⇒ Object
Also known as:
dup
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
# File 'opal/opal/corelib/hash.rb', line 211
def clone
%x{
var _map = {},
smap = {},
_map2 = self.map,
smap2 = self.smap,
keys = [],
map, map2, key, khash, value;
for (var i = 0, length = self.keys.length; i < length; i++) {
key = self.keys[i];
if (key.$$is_string) {
khash = key;
map = smap;
map2 = smap2;
} else {
khash = key.$hash();
map = _map;
map2 = _map2;
}
value = map2[khash];
keys.push(key);
map[khash] = value;
}
var clone = new self.$$class.$$alloc();
clone.map = _map;
clone.smap = smap;
clone.keys = keys;
clone.none = self.none;
clone.proc = self.proc;
return clone;
}
end
|
#default(val = undefined) ⇒ Object
251
252
253
254
255
256
257
258
|
# File 'opal/opal/corelib/hash.rb', line 251
def default(val = undefined)
%x{
if (val !== undefined && self.proc !== nil) {
return #{@proc.call(self, val)};
}
return self.none;
}
end
|
#default=(object) ⇒ Object
260
261
262
263
264
265
|
# File 'opal/opal/corelib/hash.rb', line 260
def default=(object)
%x{
self.proc = nil;
return (self.none = object);
}
end
|
#default_proc ⇒ Object
267
268
269
|
# File 'opal/opal/corelib/hash.rb', line 267
def default_proc
@proc
end
|
#default_proc=(proc) ⇒ Object
271
272
273
274
275
276
277
278
279
280
281
282
283
|
# File 'opal/opal/corelib/hash.rb', line 271
def default_proc=(proc)
%x{
if (proc !== nil) {
proc = #{Opal.coerce_to!(proc, Proc, :to_proc)};
if (#{proc.lambda?} && #{proc.arity.abs} != 2) {
#{raise TypeError, "default_proc takes two arguments"};
}
}
self.none = nil;
return (self.proc = proc);
}
end
|
#delete(key, &block) ⇒ Object
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
# File 'opal/opal/corelib/hash.rb', line 285
def delete(key, &block)
%x{
var result, map, khash;
if (key.$$is_string) {
map = self.smap;
khash = key;
} else {
map = self.map;
khash = key.$hash();
}
result = map[khash];
if (result != null) {
delete map[khash];
self.keys.$delete(key);
return result;
}
if (block !== nil) {
return #{block.call(key)};
}
return nil;
}
end
|
#delete_if(&block) ⇒ Object
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
# File 'opal/opal/corelib/hash.rb', line 313
def delete_if(&block)
return enum_for :delete_if unless block
%x{
var _map = self.map,
smap = self.smap,
keys = self.keys,
map, key, value, obj, khash;
for (var i = 0, length = keys.length; i < length; i++) {
key = keys[i];
if (key.$$is_string) {
map = smap;
khash = key;
} else {
map = _map;
khash = key.$hash();
}
obj = map[khash];
value = block(key, obj);
if (value === $breaker) {
return $breaker.$v;
}
if (value !== false && value !== nil) {
keys.splice(i, 1);
delete map[khash];
length--;
i--;
}
}
return self;
}
end
|
#each(&block) ⇒ Object
Also known as:
each_pair
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
# File 'opal/opal/corelib/hash.rb', line 354
def each(&block)
return enum_for :each unless block
%x{
var _map = self.map,
smap = self.smap,
keys = self.keys,
map, key, khash, value;
for (var i = 0, length = keys.length; i < length; i++) {
key = keys[i];
if (key.$$is_string) {
map = smap;
khash = key;
} else {
map = _map;
khash = key.$hash();
}
value = Opal.yield1(block, [key, map[khash]]);
if (value === $breaker) {
return $breaker.$v;
}
}
return self;
}
end
|
#each_key(&block) ⇒ Object
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
# File 'opal/opal/corelib/hash.rb', line 385
def each_key(&block)
return enum_for :each_key unless block
%x{
var keys = self.keys, key;
for (var i = 0, length = keys.length; i < length; i++) {
key = keys[i];
if (block(key) === $breaker) {
return $breaker.$v;
}
}
return self;
}
end
|
#each_value(&block) ⇒ Object
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
# File 'opal/opal/corelib/hash.rb', line 405
def each_value(&block)
return enum_for :each_value unless block
%x{
var _map = self.map,
smap = self.smap,
keys = self.keys, key, map, khash;
for (var i = 0, length = keys.length; i < length; i++) {
key = keys[i];
if (key.$$is_string) {
map = smap;
khash = key;
} else {
map = _map;
khash = key.$hash();
}
if (block(map[khash]) === $breaker) {
return $breaker.$v;
}
}
return self;
}
end
|
433
434
435
|
# File 'opal/opal/corelib/hash.rb', line 433
def empty?
`self.keys.length === 0`
end
|
#fetch(key, defaults = undefined, &block) ⇒ Object
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
|
# File 'opal/opal/corelib/hash.rb', line 439
def fetch(key, defaults = undefined, &block)
%x{
var map, khash, value;
if (key.$$is_string) {
khash = key;
map = self.smap;
} else {
khash = key.$hash();
map = self.map;
}
value = map[khash];
if (value != null) {
return value;
}
if (block !== nil) {
var value;
if ((value = block(key)) === $breaker) {
return $breaker.$v;
}
return value;
}
if (defaults != null) {
return defaults;
}
#{ raise KeyError, "key not found: #{key.inspect}" };
}
end
|
#flatten(level = undefined) ⇒ Object
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
|
# File 'opal/opal/corelib/hash.rb', line 475
def flatten(level=undefined)
%x{
var _map = self.map,
smap = self.smap,
keys = self.keys,
result = [],
map, key, khash, value;
for (var i = 0, length = keys.length; i < length; i++) {
key = keys[i];
if (key.$$is_string) {
khash = key;
map = smap;
} else {
khash = key.$hash();
map = _map;
}
value = map[khash];
result.push(key);
if (value.$$is_array) {
if (level == null || level === 1) {
result.push(value);
}
else {
result = result.concat(#{`value`.flatten(`level - 1`)});
}
}
else {
result.push(value);
}
}
return result;
}
end
|
#has_key?(key) ⇒ Boolean
Also known as:
include?, key?, member?
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
|
# File 'opal/opal/corelib/hash.rb', line 515
def has_key?(key)
%x{
var keys = self.keys,
map, khash;
if (key.$$is_string) {
khash = key;
map = self.smap;
} else {
khash = key.$hash();
map = self.map;
}
if (Opal.hasOwnProperty.call(map, khash)) {
for (var i = 0, length = keys.length; i < length; i++) {
if (!#{not(key.eql?(`keys[i]`))}) {
return true;
}
}
}
return false;
}
end
|
#has_value?(value) ⇒ Boolean
Also known as:
value?
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
# File 'opal/opal/corelib/hash.rb', line 540
def has_value?(value)
%x{
var _map = self.map,
smap = self.smap,
keys = self.keys, key, map, khash;
for (var i = 0, length = keys.length; i < length; i++) {
key = keys[i];
if (key.$$is_string) {
map = smap;
khash = key;
} else {
map = _map;
khash = key.$hash();
}
if (#{`map[khash]` == value}) {
return true;
}
}
return false;
}
end
|
#hash ⇒ Object
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
|
# File 'opal/opal/corelib/hash.rb', line 567
def hash
%x{
var top = (hash_ids === null);
try {
var key, value,
hash = ['Hash'],
keys = self.keys,
id = self.$object_id(),
counter = 0;
if (top) {
hash_ids = {}
}
if (hash_ids.hasOwnProperty(id)) {
return 'self';
}
hash_ids[id] = true;
for (var i = 0, length = keys.length; i < length; i++) {
key = keys[i];
value = key.$$is_string ? self.smap[key] : self.map[key.$hash()];
key = key.$hash();
value = (typeof(value) === 'undefined') ? '' : value.$hash();
hash.push([key,value]);
}
return hash.sort().join();
} finally {
if (top) {
hash_ids = null;
}
}
}
end
|
#index(object) ⇒ Object
Also known as:
key
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
|
# File 'opal/opal/corelib/hash.rb', line 606
def index(object)
%x{
var _map = self.map,
smap = self.smap,
keys = self.keys,
map, khash, key;
for (var i = 0, length = keys.length; i < length; i++) {
key = keys[i];
if (key.$$is_string) {
map = smap;
khash = key;
} else {
map = _map;
khash = key.$hash();
}
if (#{`map[khash]` == object}) {
return key;
}
}
return nil;
}
end
|
#indexes(*keys) ⇒ Object
Also known as:
indices, values_at
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
|
# File 'opal/opal/corelib/hash.rb', line 633
def indexes(*keys)
%x{
var result = [],
_map = self.map,
smap = self.smap,
map, key, khash, value;
for (var i = 0, length = keys.length; i < length; i++) {
key = keys[i];
if (key.$$is_string) {
khash = key;
map = smap;
} else {
khash = key.$hash();
map = _map;
}
value = map[khash];
if (value != null) {
result.push(value);
}
else {
result.push(self.none);
}
}
return result;
}
end
|
#inspect ⇒ Object
Also known as:
to_s
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
|
# File 'opal/opal/corelib/hash.rb', line 668
def inspect
%x{
var top = (inspect_ids === null);
try {
var key, value,
inspect = [],
keys = self.keys,
id = self.$object_id(),
counter = 0;
if (top) {
inspect_ids = {}
}
if (inspect_ids.hasOwnProperty(id)) {
return '{...}';
}
inspect_ids[id] = true;
for (var i = 0, length = keys.length; i < length; i++) {
key = keys[i];
value = key.$$is_string ? self.smap[key] : self.map[key.$hash()];
key = key.$inspect();
value = value.$inspect();
inspect.push(key + '=>' + value);
}
return '{' + inspect.join(', ') + '}';
} finally {
if (top) {
inspect_ids = null;
}
}
}
end
|
#invert ⇒ Object
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
|
# File 'opal/opal/corelib/hash.rb', line 707
def invert
%x{
var result = Opal.hash(),
keys = self.keys,
_map = self.map,
smap = self.smap,
keys2 = result.keys,
_map2 = result.map,
smap2 = result.smap,
map, map2, key, khash, value;
for (var i = 0, length = keys.length; i < length; i++) {
key = keys[i];
if (key.$$is_string) {
map = smap;
khash = key;
} else {
map = _map;
khash = key.$hash();
}
value = map[khash];
keys2.push(value);
if (value.$$is_string) {
map2 = smap2;
khash = value;
} else {
map2 = _map2;
khash = value.$hash();
}
map2[khash] = key;
}
return result;
}
end
|
#keep_if(&block) ⇒ Object
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
|
# File 'opal/opal/corelib/hash.rb', line 747
def keep_if(&block)
return enum_for :keep_if unless block
%x{
var _map = self.map,
smap = self.smap,
keys = self.keys,
map, key, khash, value, keep;
for (var i = 0, length = keys.length; i < length; i++) {
key = keys[i];
if (key.$$is_string) {
khash = key;
map = smap;
} else {
khash = key.$hash();
map = _map;
}
value = map[khash];
keep = block(key, value);
if (keep === $breaker) {
return $breaker.$v;
}
if (keep === false || keep === nil) {
keys.splice(i, 1);
delete map[khash];
length--;
i--;
}
}
return self;
}
end
|
#keys ⇒ Object
791
792
793
|
# File 'opal/opal/corelib/hash.rb', line 791
def keys
`self.keys.slice(0)`
end
|
#length ⇒ Object
Also known as:
size
795
796
797
|
# File 'opal/opal/corelib/hash.rb', line 795
def length
`self.keys.length`
end
|
#merge(other, &block) ⇒ Object
801
802
803
804
805
806
807
808
809
|
# File 'opal/opal/corelib/hash.rb', line 801
def merge(other, &block)
unless Hash === other
other = Opal.coerce_to!(other, Hash, :to_hash)
end
cloned = clone
cloned.merge!(other, &block)
cloned
end
|
#merge!(other, &block) ⇒ Object
Also known as:
update
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
|
# File 'opal/opal/corelib/hash.rb', line 811
def merge!(other, &block)
%x{
if (! #{Hash === other}) {
other = #{Opal.coerce_to!(other, Hash, :to_hash)};
}
var keys = self.keys,
_map = self.map,
smap = self.smap,
keys2 = other.keys,
_map2 = other.map,
smap2 = other.smap,
map, map2, key, khash, value, value2;
if (block === nil) {
for (var i = 0, length = keys2.length; i < length; i++) {
key = keys2[i];
if (key.$$is_string) {
khash = key;
map = smap;
map2 = smap2;
} else {
khash = key.$hash();
map = _map;
map2 = _map2;
}
if (map[khash] == null) {
keys.push(key);
}
map[khash] = map2[khash];
}
}
else {
for (var i = 0, length = keys2.length; i < length; i++) {
key = keys2[i];
if (key.$$is_string) {
khash = key;
map = smap;
map2 = smap2;
} else {
khash = key.$hash();
map = _map;
map2 = _map2;
}
value = map[khash];
value2 = map2[khash];
if (value == null) {
keys.push(key);
map[khash] = value2;
}
else {
map[khash] = block(key, value, value2);
}
}
}
return self;
}
end
|
#rassoc(object) ⇒ Object
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
|
# File 'opal/opal/corelib/hash.rb', line 877
def rassoc(object)
%x{
var keys = self.keys,
_map = self.map,
smap = self.smap,
key, khash, value, map;
for (var i = 0, length = keys.length; i < length; i++) {
key = keys[i]
if (key.$$is_string) {
khash = key;
map = smap;
} else {
khash = key.$hash();
map = _map;
}
value = map[khash];
if (#{`value` == object}) {
return [key, value];
}
}
return nil;
}
end
|
#reject(&block) ⇒ Object
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
|
# File 'opal/opal/corelib/hash.rb', line 906
def reject(&block)
return enum_for :reject unless block
%x{
var keys = self.keys,
_map = self.map,
smap = self.smap,
result = Opal.hash(),
_map2 = result.map,
smap2 = result.smap,
keys2 = result.keys,
map, map2, key, khash, object, value;
for (var i = 0, length = keys.length; i < length; i++) {
key = keys[i];
if (key.$$is_string) {
khash = key;
map = smap;
map2 = smap2;
} else {
khash = key.$hash();
map = _map;
map2 = _map2;
}
object = map[khash];
if ((value = block(key, object)) === $breaker) {
return $breaker.$v;
}
if (value === false || value === nil) {
keys2.push(key);
map2[khash] = object;
}
}
return result;
}
end
|
#replace(other) ⇒ Object
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
|
# File 'opal/opal/corelib/hash.rb', line 948
def replace(other)
%x{
var keys = self.keys = [],
_map = self.map = {},
smap = self.smap = {},
_map2 = other.map,
smap2 = other.smap,
key, khash, map, map2;
for (var i = 0, length = other.keys.length; i < length; i++) {
key = other.keys[i];
if (key.$$is_string) {
khash = key;
map = smap;
map2 = smap2;
} else {
khash = key.$hash();
map = _map;
map2 = _map2;
}
keys.push(key);
map[khash] = map2[khash];
}
return self;
}
end
|
#select(&block) ⇒ Object
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
|
# File 'opal/opal/corelib/hash.rb', line 978
def select(&block)
return enum_for :select unless block
%x{
var keys = self.keys,
_map = self.map,
smap = self.smap,
result = Opal.hash(),
_map2 = result.map,
smap2 = result.smap,
keys2 = result.keys,
map, map2, key, khash, value, object;
for (var i = 0, length = keys.length; i < length; i++) {
key = keys[i];
if (key.$$is_string) {
khash = key;
map = smap;
map2 = smap2;
} else {
khash = key.$hash();
map = _map;
map2 = _map2;
}
value = map[khash];
object = block(key, value);
if (object === $breaker) {
return $breaker.$v;
}
if (object !== false && object !== nil) {
keys2.push(key);
map2[khash] = value;
}
}
return result;
}
end
|
#select!(&block) ⇒ Object
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
|
# File 'opal/opal/corelib/hash.rb', line 1021
def select!(&block)
return enum_for :select! unless block
%x{
var _map = self.map,
smap = self.smap,
keys = self.keys,
result = nil,
key, khash, value, object, map;
for (var i = 0, length = keys.length; i < length; i++) {
key = keys[i];
if (key.$$is_string) {
khash = key;
map = smap;
} else {
khash = key.$hash();
map = _map;
}
value = map[khash];
object = block(key, value);
if (object === $breaker) {
return $breaker.$v;
}
if (object === false || object === nil) {
keys.splice(i, 1);
delete map[khash];
length--;
i--;
result = self
}
}
return result;
}
end
|
#shift ⇒ Object
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
|
# File 'opal/opal/corelib/hash.rb', line 1063
def shift
%x{
var keys = self.keys,
_map = self.map,
smap = self.smap,
map, key, khash, value;
if (keys.length) {
key = keys[0];
if (key.$$is_string) {
khash = key;
map = smap;
} else {
khash = key.$hash();
map = _map;
}
value = map[khash];
delete map[khash];
keys.splice(0, 1);
return [key, value];
}
return nil;
}
end
|
#to_a ⇒ Object
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
|
# File 'opal/opal/corelib/hash.rb', line 1095
def to_a
%x{
var keys = self.keys,
_map = self.map,
smap = self.smap,
result = [],
map, key, khash;
for (var i = 0, length = keys.length; i < length; i++) {
key = keys[i];
if (key.$$is_string) {
khash = key;
map = smap;
} else {
khash = key.$hash();
map = _map;
}
result.push([key, map[khash]]);
}
return result;
}
end
|
#to_h ⇒ Object
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
|
# File 'opal/opal/corelib/hash.rb', line 1121
def to_h
%x{
if (self.$$class === Opal.Hash) {
return self
}
var hash = new Opal.Hash.$$alloc,
cloned = #{clone};
hash.map = cloned.map;
hash.smap = cloned.smap;
hash.keys = cloned.keys;
hash.none = cloned.none;
hash.proc = cloned.proc;
return hash;
}
end
|
#to_hash ⇒ Object
1140
1141
1142
|
# File 'opal/opal/corelib/hash.rb', line 1140
def to_hash
self
end
|
#values ⇒ Object
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
|
# File 'opal/opal/corelib/hash.rb', line 1152
def values
%x{
var _map = self.map,
smap = self.smap,
keys = self.keys,
result = [],
map, khash, key;
for (var i = 0, length = keys.length; i < length; i++) {
key = keys[i];
if (key.$$is_string) {
khash = key;
map = smap;
} else {
khash = key.$hash();
map = _map;
}
result.push(map[khash]);
}
return result;
}
end
|