Class: Hash

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
opal/opal/corelib/hash.rb

Class Method Summary collapse

Instance Method Summary collapse

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



27
28
29
30
31
32
33
# File 'opal/opal/corelib/hash.rb', line 27

def initialize(defaults = undefined, &block)
  %x{
    self.none = (defaults === undefined ? nil : defaults);
    self.proc = block;
  }
  self
end

Class Method Details

.[](*objs) ⇒ Object



9
10
11
# File 'opal/opal/corelib/hash.rb', line 9

def self.[](*objs)
  `Opal.hash.apply(null, objs)`
end

.allocateObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'opal/opal/corelib/hash.rb', line 13

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?



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'opal/opal/corelib/hash.rb', line 35

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



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
# File 'opal/opal/corelib/hash.rb', line 81

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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'opal/opal/corelib/hash.rb', line 110

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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'opal/opal/corelib/hash.rb', line 132

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

#clearObject



157
158
159
160
161
162
163
164
# File 'opal/opal/corelib/hash.rb', line 157

def clear
  %x{
    self.map = {};
    self.smap = {};
    self.keys = [];
    return self;
  }
end

#cloneObject Also known as: dup



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'opal/opal/corelib/hash.rb', line 166

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



206
207
208
209
210
211
212
213
# File 'opal/opal/corelib/hash.rb', line 206

def default(val = undefined)
  %x{
    if (val !== undefined && self.proc !== nil) {
      return #{@proc.call(self, val)};
    }
    return self.none;
  }
end

#default=(object) ⇒ Object



215
216
217
218
219
220
# File 'opal/opal/corelib/hash.rb', line 215

def default=(object)
  %x{
    self.proc = nil;
    return (self.none = object);
  }
end

#default_procObject



222
223
224
# File 'opal/opal/corelib/hash.rb', line 222

def default_proc
  @proc
end

#default_proc=(proc) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'opal/opal/corelib/hash.rb', line 226

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



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'opal/opal/corelib/hash.rb', line 240

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



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'opal/opal/corelib/hash.rb', line 268

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



309
310
311
312
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
# File 'opal/opal/corelib/hash.rb', line 309

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



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'opal/opal/corelib/hash.rb', line 340

def each_key(&block)
  return enum_for :each_key unless block
  # @keys.each(&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



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'opal/opal/corelib/hash.rb', line 360

def each_value(&block)
  return enum_for :each_value unless block

  %x{
    var _map = self.map,
        smap = self.smap,
        keys = self.keys;

    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

#empty?Boolean

Returns:



388
389
390
# File 'opal/opal/corelib/hash.rb', line 388

def empty?
  `self.keys.length === 0`
end

#fetch(key, defaults = undefined, &block) ⇒ Object



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'opal/opal/corelib/hash.rb', line 394

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" };
  }
end

#flatten(level = undefined) ⇒ Object



430
431
432
433
434
435
436
437
438
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
# File 'opal/opal/corelib/hash.rb', line 430

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?

Returns:



470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'opal/opal/corelib/hash.rb', line 470

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?

Returns:



495
496
497
498
499
500
501
502
503
504
505
# File 'opal/opal/corelib/hash.rb', line 495

def has_value?(value)
  %x{
    for (var khash in self.map) {
      if (#{`self.map[khash]` == value}) {
        return true;
      }
    }

    return false;
  }
end

#hashObject



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'opal/opal/corelib/hash.rb', line 508

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



547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'opal/opal/corelib/hash.rb', line 547

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



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
603
604
# File 'opal/opal/corelib/hash.rb', line 574

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

#inspectObject Also known as: to_s



609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'opal/opal/corelib/hash.rb', line 609

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

#invertObject



648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
# File 'opal/opal/corelib/hash.rb', line 648

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



688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
# File 'opal/opal/corelib/hash.rb', line 688

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

#keysObject



732
733
734
# File 'opal/opal/corelib/hash.rb', line 732

def keys
  `self.keys.slice(0)`
end

#lengthObject Also known as: size



736
737
738
# File 'opal/opal/corelib/hash.rb', line 736

def length
  `self.keys.length`
end

#merge(other, &block) ⇒ Object



742
743
744
745
746
747
748
749
750
# File 'opal/opal/corelib/hash.rb', line 742

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



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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
# File 'opal/opal/corelib/hash.rb', line 752

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



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
# File 'opal/opal/corelib/hash.rb', line 818

def rassoc(object)
  %x{
    var keys = self.keys,
        _map = self.map,
        smap = self.smap,
        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` == object}) {
        return [key, value];
      }
    }

    return nil;
  }
end

#reject(&block) ⇒ Object



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
876
877
878
879
880
881
882
883
884
885
886
887
# File 'opal/opal/corelib/hash.rb', line 847

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



889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
# File 'opal/opal/corelib/hash.rb', line 889

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



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
947
948
949
950
951
952
953
954
955
956
957
958
959
960
# File 'opal/opal/corelib/hash.rb', line 919

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



962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
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
# File 'opal/opal/corelib/hash.rb', line 962

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;

    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

#shiftObject



1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
# File 'opal/opal/corelib/hash.rb', line 1004

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_aObject



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
# File 'opal/opal/corelib/hash.rb', line 1036

def to_a
  %x{
    var keys = self.keys,
        _map = self.map,
        smap = self.smap,
        result = [],
        map, 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([key, map[khash]]);
    }

    return result;
  }
end

#to_hObject



1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
# File 'opal/opal/corelib/hash.rb', line 1062

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_hashObject



1081
1082
1083
# File 'opal/opal/corelib/hash.rb', line 1081

def to_hash
  self
end

#valuesObject



1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
# File 'opal/opal/corelib/hash.rb', line 1093

def values
  %x{
    var _map = self.map,
        smap = self.smap,
        keys = self.keys,
        result = [],
        map, 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(map[khash]);
    }

    return result;
  }
end