Class: Hash

Inherits:
Object show all
Includes:
Enumerable
Defined in:
opal/opal/corelib/hash.rb,
opal/opal/corelib/marshal/write_buffer.rb,
opal/opal/corelib/pattern_matching/base.rb

Overview


Internal properties:

  • $$map [JS::Object hash-bucket>] the hash table for ordinary keys
  • $$smap [JS::Object hash-bucket>] the hash table for string keys
  • $$keys [Array] the list of all keys
  • $$proc [Proc,null,nil] the default proc used for missing keys
  • hash-bucket [JS::Object] an element of a linked list that holds hash values, keys are {key:,key_hash:,value:,next:}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#all?, #any?, #chunk, #chunk_while, #collect, #collect_concat, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_entry, #each_slice, #each_with_index, #each_with_object, #entries, #enumerator_size, #filter_map, #find_all, #find_index, #first, #grep, #grep_v, #group_by, #inject, #lazy, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reverse_each, #slice_after, #slice_before, #slice_when, #sort, #sort_by, #sum, #take, #take_while, #tally, #to_set, #uniq, #zip

Constructor Details

#initialize(defaults = undefined, &block) ⇒ Hash

Returns a new instance of Hash.



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'opal/opal/corelib/hash.rb', line 85

def initialize(defaults = undefined, &block)
  %x{
    $deny_frozen_access(self);

    if (defaults !== undefined && block !== nil) {
      #{::Kernel.raise ::ArgumentError, 'wrong number of arguments (1 for 0)'}
    }
    self.$$none = (defaults === undefined ? nil : defaults);
    self.$$proc = block;

    return self;
  }
end

Class Method Details

.[](*argv) ⇒ Object



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
57
58
59
60
61
62
63
64
65
66
# File 'opal/opal/corelib/hash.rb', line 19

def self.[](*argv)
  %x{
    var hash, argc = argv.length, i;

    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) {
        #{::Kernel.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:
          #{::Kernel.raise ::ArgumentError, "invalid number of elements (#{`argv[i].length`} for 1..2)"}
        }
      }

      return hash;
    }

    if (argc % 2 !== 0) {
      #{::Kernel.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

.allocateObject



68
69
70
71
72
73
74
75
76
77
78
79
# File 'opal/opal/corelib/hash.rb', line 68

def self.allocate
  %x{
    var hash = new self.$$constructor();

    $hash_init(hash);

    hash.$$none = nil;
    hash.$$proc = nil;

    return hash;
  }
end

.try_convert(obj) ⇒ Object



81
82
83
# File 'opal/opal/corelib/hash.rb', line 81

def self.try_convert(obj)
  ::Opal.coerce_to?(obj, ::Hash, :to_hash)
end

Instance Method Details

#<(other) ⇒ Object



170
171
172
173
# File 'opal/opal/corelib/hash.rb', line 170

def <(other)
  other = ::Opal.coerce_to!(other, ::Hash, :to_hash)
  other > self
end

#<=(other) ⇒ Object



175
176
177
178
# File 'opal/opal/corelib/hash.rb', line 175

def <=(other)
  other = ::Opal.coerce_to!(other, ::Hash, :to_hash)
  other >= self
end

#==(other) ⇒ Object Also known as: eql?



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
125
126
127
128
129
130
131
# File 'opal/opal/corelib/hash.rb', line 99

def ==(other)
  %x{
    if (self === other) {
      return true;
    }

    if (!other.$$is_hash) {
      return false;
    }

    if (self.$$keys.length !== other.$$keys.length) {
      return false;
    }

    for (var i = 0, keys = self.$$keys, length = keys.length, key, value, other_value; i < length; i++) {
      key = keys[i];

      if (key.$$is_string) {
        value = self.$$smap[key];
        other_value = other.$$smap[key];
      } else {
        value = key.value;
        other_value = $hash_get(other, key.key);
      }

      if (other_value === undefined || !value['$eql?'](other_value)) {
        return false;
      }
    }

    return true;
  }
end

#>(other) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
# File 'opal/opal/corelib/hash.rb', line 158

def >(other)
  other = ::Opal.coerce_to!(other, ::Hash, :to_hash)

  %x{
    if (self.$$keys.length <= other.$$keys.length) {
      return false
    }
  }

  self >= other
end

#>=(other) ⇒ Object



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

def >=(other)
  other = ::Opal.coerce_to!(other, ::Hash, :to_hash)

  %x{
    if (self.$$keys.length < other.$$keys.length) {
      return false
    }
  }

  result = true

  other.each do |other_key, other_val|
    val = fetch(other_key, `null`)

    %x{
      if (val == null || val !== other_val) {
        result = false;
        return;
      }
    }
  end

  result
end

#[](key) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
# File 'opal/opal/corelib/hash.rb', line 180

def [](key)
  %x{
    var value = $hash_get(self, key);

    if (value !== undefined) {
      return value;
    }

    return self.$default(key);
  }
end

#[]=(key, value) ⇒ Object Also known as: store



192
193
194
195
196
197
198
199
# File 'opal/opal/corelib/hash.rb', line 192

def []=(key, value)
  %x{
    $deny_frozen_access(self);

    $hash_put(self, key, value);
    return value;
  }
end

#__marshal__(buffer) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 61

def __marshal__(buffer)
  if default_proc
    ::Kernel.raise ::TypeError, "can't dump hash with default proc"
  end

  buffer.save_link(self)
  buffer.write_ivars_prefix(self)
  buffer.write_extends(self)
  buffer.write_user_class(Hash, self)
  if default
    buffer.append('}')
    buffer.write_hash(self)
    buffer.write(default)
  else
    buffer.append('{')
    buffer.write_hash(self)
  end
  buffer.write_ivars_suffix(self)
end

#assoc(object) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'opal/opal/corelib/hash.rb', line 201

def assoc(object)
  %x{
    for (var i = 0, keys = self.$$keys, length = keys.length, key; i < length; i++) {
      key = keys[i];

      if (key.$$is_string) {
        if (#{`key` == object}) {
          return [key, self.$$smap[key]];
        }
      } else {
        if (#{`key.key` == object}) {
          return [key.key, key.value];
        }
      }
    }

    return nil;
  }
end

#clearObject



221
222
223
224
225
226
227
228
# File 'opal/opal/corelib/hash.rb', line 221

def clear
  %x{
    $deny_frozen_access(self);

    $hash_init(self);
    return self;
  }
end

#cloneObject Also known as: dup



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

def clone
  %x{
    var hash = new self.$$class();

    $hash_init(hash);
    Opal.hash_clone(self, hash);

    return hash;
  }
end

#compactObject



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

def compact
  %x{
    var hash = $hash();

    for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) {
      key = keys[i];

      if (key.$$is_string) {
        value = self.$$smap[key];
      } else {
        value = key.value;
        key = key.key;
      }

      if (value !== nil) {
        $hash_put(hash, key, value);
      }
    }

    return hash;
  }
end

#compact!Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'opal/opal/corelib/hash.rb', line 264

def compact!
  %x{
    $deny_frozen_access(self);

    var changes_were_made = false;

    for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) {
      key = keys[i];

      if (key.$$is_string) {
        value = self.$$smap[key];
      } else {
        value = key.value;
        key = key.key;
      }

      if (value === nil) {
        if ($hash_delete(self, key) !== undefined) {
          changes_were_made = true;
          length--;
          i--;
        }
      }
    }

    return changes_were_made ? self : nil;
  }
end

#compare_by_identityObject



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'opal/opal/corelib/hash.rb', line 293

def compare_by_identity
  %x{
    $deny_frozen_access(self);

    var i, ii, key, keys = self.$$keys, identity_hash;

    if (self.$$by_identity) return self;
    if (self.$$keys.length === 0) {
      self.$$by_identity = true
      return self;
    }

    identity_hash = #{ {}.compare_by_identity };
    for(i = 0, ii = keys.length; i < ii; i++) {
      key = keys[i];
      if (!key.$$is_string) key = key.key;
      $hash_put(identity_hash, key, $hash_get(self, key));
    }

    self.$$by_identity = true;
    self.$$map = identity_hash.$$map;
    self.$$smap = identity_hash.$$smap;
    return self;
  }
end

#compare_by_identity?Boolean

Returns:



319
320
321
# File 'opal/opal/corelib/hash.rb', line 319

def compare_by_identity?
  `self.$$by_identity === true`
end

#deconstruct_keys(_) ⇒ Object



8
9
10
# File 'opal/opal/corelib/pattern_matching/base.rb', line 8

def deconstruct_keys(_)
  self
end

#default(key = undefined) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
# File 'opal/opal/corelib/hash.rb', line 323

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

#default=(object) ⇒ Object



335
336
337
338
339
340
341
342
343
344
# File 'opal/opal/corelib/hash.rb', line 335

def default=(object)
  %x{
    $deny_frozen_access(self);

    self.$$proc = nil;
    self.$$none = object;

    return object;
  }
end

#default_procObject



346
347
348
349
350
351
352
353
# File 'opal/opal/corelib/hash.rb', line 346

def default_proc
  %x{
    if (self.$$proc !== undefined) {
      return self.$$proc;
    }
    return nil;
  }
end

#default_proc=(default_proc) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'opal/opal/corelib/hash.rb', line 355

def default_proc=(default_proc)
  %x{
    $deny_frozen_access(self);

    var proc = default_proc;

    if (proc !== nil) {
      proc = #{::Opal.coerce_to!(`proc`, ::Proc, :to_proc)};

      if (#{`proc`.lambda?} && #{`proc`.arity.abs} !== 2) {
        #{::Kernel.raise ::TypeError, 'default_proc takes two arguments'};
      }
    }

    self.$$none = nil;
    self.$$proc = proc;

    return default_proc;
  }
end

#delete(key, &block) ⇒ Object



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'opal/opal/corelib/hash.rb', line 376

def delete(key, &block)
  %x{
    $deny_frozen_access(self);
    var value = $hash_delete(self, key);

    if (value !== undefined) {
      return value;
    }

    if (block !== nil) {
      return #{yield key};
    }

    return nil;
  }
end

#delete_if(&block) ⇒ Object



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

def delete_if(&block)
  return enum_for(:delete_if) { size } unless block

  %x{
    $deny_frozen_access(self);

    for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) {
      key = keys[i];

      if (key.$$is_string) {
        value = self.$$smap[key];
      } else {
        value = key.value;
        key = key.key;
      }

      obj = block(key, value);

      if (obj !== false && obj !== nil) {
        if ($hash_delete(self, key) !== undefined) {
          length--;
          i--;
        }
      }
    }

    return self;
  }
end

#dig(key, *keys) ⇒ Object



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'opal/opal/corelib/hash.rb', line 423

def dig(key, *keys)
  item = self[key]

  %x{
    if (item === nil || keys.length === 0) {
      return item;
    }
  }

  unless item.respond_to?(:dig)
    ::Kernel.raise ::TypeError, "#{item.class} does not have #dig method"
  end

  item.dig(*keys)
end

#each(&block) ⇒ Object Also known as: each_pair



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'opal/opal/corelib/hash.rb', line 439

def each(&block)
  return enum_for(:each) { size } unless block

  %x{
    for (var i = 0, keys = self.$$keys.slice(), length = keys.length, key, value; i < length; i++) {
      key = keys[i];

      if (key.$$is_string) {
        value = self.$$smap[key];
      } else {
        value = key.value;
        key = key.key;
      }

      $yield1(block, [key, value]);
    }

    return self;
  }
end

#each_key(&block) ⇒ Object



460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'opal/opal/corelib/hash.rb', line 460

def each_key(&block)
  return enum_for(:each_key) { size } unless block

  %x{
    for (var i = 0, keys = self.$$keys.slice(), length = keys.length, key; i < length; i++) {
      key = keys[i];

      block(key.$$is_string ? key : key.key);
    }

    return self;
  }
end

#each_value(&block) ⇒ Object



474
475
476
477
478
479
480
481
482
483
484
485
486
# File 'opal/opal/corelib/hash.rb', line 474

def each_value(&block)
  return enum_for(:each_value) { size } unless block

  %x{
    for (var i = 0, keys = self.$$keys.slice(), length = keys.length, key; i < length; i++) {
      key = keys[i];

      block(key.$$is_string ? self.$$smap[key] : key.value);
    }

    return self;
  }
end

#empty?Boolean

Returns:



488
489
490
# File 'opal/opal/corelib/hash.rb', line 488

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

#except(*keys) ⇒ Object



492
493
494
# File 'opal/opal/corelib/hash.rb', line 492

def except(*keys)
  dup.except!(*keys)
end

#except!(*keys) ⇒ Object



496
497
498
499
# File 'opal/opal/corelib/hash.rb', line 496

def except!(*keys)
  keys.each { |key| delete(key) }
  self
end

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



501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'opal/opal/corelib/hash.rb', line 501

def fetch(key, defaults = undefined, &block)
  %x{
    var value = $hash_get(self, key);

    if (value !== undefined) {
      return value;
    }

    if (block !== nil) {
      return block(key);
    }

    if (defaults !== undefined) {
      return defaults;
    }
  }

  ::Kernel.raise ::KeyError.new("key not found: #{key.inspect}", key: key, receiver: self)
end

#fetch_values(*keys, &block) ⇒ Object



521
522
523
# File 'opal/opal/corelib/hash.rb', line 521

def fetch_values(*keys, &block)
  keys.map { |key| fetch(key, &block) }
end

#flatten(level = 1) ⇒ Object



525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'opal/opal/corelib/hash.rb', line 525

def flatten(level = 1)
  level = ::Opal.coerce_to!(level, ::Integer, :to_int)

  %x{
    var result = [];

    for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) {
      key = keys[i];

      if (key.$$is_string) {
        value = self.$$smap[key];
      } else {
        value = key.value;
        key = key.key;
      }

      result.push(key);

      if (value.$$is_array) {
        if (level === 1) {
          result.push(value);
          continue;
        }

        result = result.concat(#{`value`.flatten(`level - 2`)});
        continue;
      }

      result.push(value);
    }

    return result;
  }
end

#freezeObject



560
561
562
563
564
# File 'opal/opal/corelib/hash.rb', line 560

def freeze
  return self if frozen?

  `$freeze(self)`
end

#has_key?(key) ⇒ Boolean Also known as: include?, key?, member?

Returns:



566
567
568
# File 'opal/opal/corelib/hash.rb', line 566

def has_key?(key)
  `$hash_get(self, key) !== undefined`
end

#has_value?(value) ⇒ Boolean Also known as: value?

Returns:



570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'opal/opal/corelib/hash.rb', line 570

def has_value?(value)
  %x{
    for (var i = 0, keys = self.$$keys, length = keys.length, key; i < length; i++) {
      key = keys[i];

      if (#{`(key.$$is_string ? self.$$smap[key] : key.value)` == value}) {
        return true;
      }
    }

    return false;
  }
end

#hashObject



584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# File 'opal/opal/corelib/hash.rb', line 584

def hash
  %x{
    var top = (Opal.hash_ids === undefined),
        hash_id = self.$object_id(),
        result = ['Hash'],
        key, item;

    try {
      if (top) {
        Opal.hash_ids = Object.create(null);
      }

      if (Opal[hash_id]) {
        return 'self';
      }

      for (key in Opal.hash_ids) {
        item = Opal.hash_ids[key];
        if (#{eql?(`item`)}) {
          return 'self';
        }
      }

      Opal.hash_ids[hash_id] = self;

      for (var i = 0, keys = self.$$keys, length = keys.length; i < length; i++) {
        key = keys[i];

        if (key.$$is_string) {
          result.push([key, self.$$smap[key].$hash()]);
        } else {
          result.push([key.key_hash, key.value.$hash()]);
        }
      }

      return result.sort().join();

    } finally {
      if (top) {
        Opal.hash_ids = undefined;
      }
    }
  }
end

#index(object) ⇒ Object Also known as: key



629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
# File 'opal/opal/corelib/hash.rb', line 629

def index(object)
  %x{
    for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) {
      key = keys[i];

      if (key.$$is_string) {
        value = self.$$smap[key];
      } else {
        value = key.value;
        key = key.key;
      }

      if (#{`value` == object}) {
        return key;
      }
    }

    return nil;
  }
end

#indexes(*args) ⇒ Object Also known as: indices, values_at



650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'opal/opal/corelib/hash.rb', line 650

def indexes(*args)
  %x{
    var result = [];

    for (var i = 0, length = args.length, key, value; i < length; i++) {
      key = args[i];
      value = $hash_get(self, key);

      if (value === undefined) {
        result.push(#{default});
        continue;
      }

      result.push(value);
    }

    return result;
  }
end

#inspectObject Also known as: to_s



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
706
707
708
709
710
711
712
713
# File 'opal/opal/corelib/hash.rb', line 672

def inspect
  %x{
    var top = (inspect_ids === undefined),
        hash_id = self.$object_id(),
        result = [];
  }

  begin
    %x{
      if (top) {
        inspect_ids = {};
      }

      if (inspect_ids.hasOwnProperty(hash_id)) {
        return '{...}';
      }

      inspect_ids[hash_id] = true;

      for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) {
        key = keys[i];

        if (key.$$is_string) {
          value = self.$$smap[key];
        } else {
          value = key.value;
          key = key.key;
        }

        key = #{Opal.inspect(`key`)}
        value = #{Opal.inspect(`value`)}

        result.push(key + '=>' + value);
      }

      return '{' + result.join(', ') + '}';
    }
    nil
  ensure
    `if (top) inspect_ids = undefined`
  end
end

#invertObject



715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
# File 'opal/opal/corelib/hash.rb', line 715

def invert
  %x{
    var hash = $hash();

    for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) {
      key = keys[i];

      if (key.$$is_string) {
        value = self.$$smap[key];
      } else {
        value = key.value;
        key = key.key;
      }

      $hash_put(hash, value, key);
    }

    return hash;
  }
end

#keep_if(&block) ⇒ Object



736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
# File 'opal/opal/corelib/hash.rb', line 736

def keep_if(&block)
  return enum_for(:keep_if) { size } unless block

  %x{
    $deny_frozen_access(self);

    for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) {
      key = keys[i];

      if (key.$$is_string) {
        value = self.$$smap[key];
      } else {
        value = key.value;
        key = key.key;
      }

      obj = block(key, value);

      if (obj === false || obj === nil) {
        if ($hash_delete(self, key) !== undefined) {
          length--;
          i--;
        }
      }
    }

    return self;
  }
end

#keysObject



766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
# File 'opal/opal/corelib/hash.rb', line 766

def keys
  %x{
    var result = [];

    for (var i = 0, keys = self.$$keys, length = keys.length, key; i < length; i++) {
      key = keys[i];

      if (key.$$is_string) {
        result.push(key);
      } else {
        result.push(key.key);
      }
    }

    return result;
  }
end

#lengthObject Also known as: size



784
785
786
# File 'opal/opal/corelib/hash.rb', line 784

def length
  `self.$$keys.length`
end

#merge(*others, &block) ⇒ Object



788
789
790
# File 'opal/opal/corelib/hash.rb', line 788

def merge(*others, &block)
  dup.merge!(*others, &block)
end

#merge!(*others, &block) ⇒ Object Also known as: update



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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
# File 'opal/opal/corelib/hash.rb', line 792

def merge!(*others, &block)
  %x{
    $deny_frozen_access(self);
    var i, j, other, other_keys, length, key, value, other_value;
    for (i = 0; i < others.length; ++i) {
      other = #{::Opal.coerce_to!(`others[i]`, ::Hash, :to_hash)};
      other_keys = other.$$keys, length = other_keys.length;

      if (block === nil) {
        for (j = 0; j < length; j++) {
          key = other_keys[j];

          if (key.$$is_string) {
            other_value = other.$$smap[key];
          } else {
            other_value = key.value;
            key = key.key;
          }

          $hash_put(self, key, other_value);
        }
      } else {
        for (j = 0; j < length; j++) {
          key = other_keys[j];

          if (key.$$is_string) {
            other_value = other.$$smap[key];
          } else {
            other_value = key.value;
            key = key.key;
          }

          value = $hash_get(self, key);

          if (value === undefined) {
            $hash_put(self, key, other_value);
            continue;
          }

          $hash_put(self, key, block(key, value, other_value));
        }
      }
    }

    return self;
  }
end

#rassoc(object) ⇒ Object



840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
# File 'opal/opal/corelib/hash.rb', line 840

def rassoc(object)
  %x{
    for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) {
      key = keys[i];

      if (key.$$is_string) {
        value = self.$$smap[key];
      } else {
        value = key.value;
        key = key.key;
      }

      if (#{`value` == object}) {
        return [key, value];
      }
    }

    return nil;
  }
end

#rehashObject



861
862
863
864
865
866
867
# File 'opal/opal/corelib/hash.rb', line 861

def rehash
  %x{
    $deny_frozen_access(self);
    Opal.hash_rehash(self);
    return self;
  }
end

#reject(&block) ⇒ Object



869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
# File 'opal/opal/corelib/hash.rb', line 869

def reject(&block)
  return enum_for(:reject) { size } unless block

  %x{
    var hash = $hash();

    for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) {
      key = keys[i];

      if (key.$$is_string) {
        value = self.$$smap[key];
      } else {
        value = key.value;
        key = key.key;
      }

      obj = block(key, value);

      if (obj === false || obj === nil) {
        $hash_put(hash, key, value);
      }
    }

    return hash;
  }
end

#reject!(&block) ⇒ Object



896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
# File 'opal/opal/corelib/hash.rb', line 896

def reject!(&block)
  return enum_for(:reject!) { size } unless block

  %x{
    $deny_frozen_access(self);

    var changes_were_made = false;

    for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) {
      key = keys[i];

      if (key.$$is_string) {
        value = self.$$smap[key];
      } else {
        value = key.value;
        key = key.key;
      }

      obj = block(key, value);

      if (obj !== false && obj !== nil) {
        if ($hash_delete(self, key) !== undefined) {
          changes_were_made = true;
          length--;
          i--;
        }
      }
    }

    return changes_were_made ? self : nil;
  }
end

#replace(other) ⇒ Object



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

def replace(other)
  `$deny_frozen_access(self);`

  other = ::Opal.coerce_to!(other, ::Hash, :to_hash)

  %x{
    $hash_init(self);

    for (var i = 0, other_keys = other.$$keys, length = other_keys.length, key, value, other_value; i < length; i++) {
      key = other_keys[i];

      if (key.$$is_string) {
        other_value = other.$$smap[key];
      } else {
        other_value = key.value;
        key = key.key;
      }

      $hash_put(self, key, other_value);
    }
  }

  if other.default_proc
    self.default_proc = other.default_proc
  else
    self.default = other.default
  end

  self
end

#select(&block) ⇒ Object Also known as: filter



960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
# File 'opal/opal/corelib/hash.rb', line 960

def select(&block)
  return enum_for(:select) { size } unless block

  %x{
    var hash = $hash();

    for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) {
      key = keys[i];

      if (key.$$is_string) {
        value = self.$$smap[key];
      } else {
        value = key.value;
        key = key.key;
      }

      obj = block(key, value);

      if (obj !== false && obj !== nil) {
        $hash_put(hash, key, value);
      }
    }

    return hash;
  }
end

#select!(&block) ⇒ Object Also known as: filter!



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

def select!(&block)
  return enum_for(:select!) { size } unless block

  %x{
    $deny_frozen_access(self);

    var result = nil;

    for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) {
      key = keys[i];

      if (key.$$is_string) {
        value = self.$$smap[key];
      } else {
        value = key.value;
        key = key.key;
      }

      obj = block(key, value);

      if (obj === false || obj === nil) {
        if ($hash_delete(self, key) !== undefined) {
          length--;
          i--;
        }
        result = self;
      }
    }

    return result;
  }
end

#shiftObject



1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
# File 'opal/opal/corelib/hash.rb', line 1020

def shift
  %x{
    $deny_frozen_access(self);
    var keys = self.$$keys,
        key;

    if (keys.length > 0) {
      key = keys[0];

      key = key.$$is_string ? key : key.key;

      return [key, $hash_delete(self, key)];
    }

    return nil;
  }
end

#slice(*keys) ⇒ Object



1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
# File 'opal/opal/corelib/hash.rb', line 1038

def slice(*keys)
  %x{
    var result = $hash();

    for (var i = 0, length = keys.length; i < length; i++) {
      var key = keys[i], value = $hash_get(self, key);

      if (value !== undefined) {
        $hash_put(result, key, value);
      }
    }

    return result;
  }
end

#to_aObject



1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
# File 'opal/opal/corelib/hash.rb', line 1054

def to_a
  %x{
    var result = [];

    for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) {
      key = keys[i];

      if (key.$$is_string) {
        value = self.$$smap[key];
      } else {
        value = key.value;
        key = key.key;
      }

      result.push([key, value]);
    }

    return result;
  }
end

#to_h(&block) ⇒ Object



1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
# File 'opal/opal/corelib/hash.rb', line 1075

def to_h(&block)
  return map(&block).to_h if block_given?

  %x{
    if (self.$$class === Opal.Hash) {
      return self;
    }

    var hash = new Opal.Hash();

    $hash_init(hash);
    Opal.hash_clone(self, hash);

    return hash;
  }
end

#to_hashObject



1092
1093
1094
# File 'opal/opal/corelib/hash.rb', line 1092

def to_hash
  self
end

#to_procObject



1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
# File 'opal/opal/corelib/hash.rb', line 1096

def to_proc
  proc do |key = undefined|
    %x{
      if (key == null) {
        #{::Kernel.raise ::ArgumentError, 'no key given'}
      }
    }

    self[key]
  end
end

#transform_keys(&block) ⇒ Object



1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
# File 'opal/opal/corelib/hash.rb', line 1108

def transform_keys(&block)
  return enum_for(:transform_keys) { size } unless block

  %x{
    var result = $hash();

    for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) {
      key = keys[i];

      if (key.$$is_string) {
        value = self.$$smap[key];
      } else {
        value = key.value;
        key = key.key;
      }

      key = $yield1(block, key);

      $hash_put(result, key, value);
    }

    return result;
  }
end

#transform_keys!(&block) ⇒ Object



1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
# File 'opal/opal/corelib/hash.rb', line 1133

def transform_keys!(&block)
  return enum_for(:transform_keys!) { size } unless block

  %x{
    $deny_frozen_access(self);

    var keys = Opal.slice(self.$$keys),
        i, length = keys.length, key, value, new_key;

    for (i = 0; i < length; i++) {
      key = keys[i];

      if (key.$$is_string) {
        value = self.$$smap[key];
      } else {
        value = key.value;
        key = key.key;
      }

      new_key = $yield1(block, key);

      $hash_delete(self, key);
      $hash_put(self, new_key, value);
    }

    return self;
  }
end

#transform_values(&block) ⇒ Object



1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
# File 'opal/opal/corelib/hash.rb', line 1162

def transform_values(&block)
  return enum_for(:transform_values) { size } unless block

  %x{
    var result = $hash();

    for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) {
      key = keys[i];

      if (key.$$is_string) {
        value = self.$$smap[key];
      } else {
        value = key.value;
        key = key.key;
      }

      value = $yield1(block, value);

      $hash_put(result, key, value);
    }

    return result;
  }
end

#transform_values!(&block) ⇒ Object



1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
# File 'opal/opal/corelib/hash.rb', line 1187

def transform_values!(&block)
  return enum_for(:transform_values!) { size } unless block

  %x{
    $deny_frozen_access(self);

    for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) {
      key = keys[i];

      if (key.$$is_string) {
        value = self.$$smap[key];
      } else {
        value = key.value;
        key = key.key;
      }

      value = $yield1(block, value);

      $hash_put(self, key, value);
    }

    return self;
  }
end

#valuesObject



1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
# File 'opal/opal/corelib/hash.rb', line 1212

def values
  %x{
    var result = [];

    for (var i = 0, keys = self.$$keys, length = keys.length, key; i < length; i++) {
      key = keys[i];

      if (key.$$is_string) {
        result.push(self.$$smap[key]);
      } else {
        result.push(key.value);
      }
    }

    return result;
  }
end