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



23
24
25
26
27
28
29
# File 'opal/opal/corelib/hash.rb', line 23

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

Class Method Details

.[](*objs) ⇒ Object



6
7
8
# File 'opal/opal/corelib/hash.rb', line 6

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

.allocateObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'opal/opal/corelib/hash.rb', line 10

def self.allocate
  %x{
    var hash = new self._alloc;

    hash.map  = {};
    hash.keys = [];
    hash.none = nil;
    hash.proc = nil;

    return hash;
  }
end

Instance Method Details

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



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

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

    if (!other.map || !other.keys) {
      return false;
    }

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

    var map  = self.map,
        map2 = other.map;

    for (var i = 0, length = self.keys.length; i < length; i++) {
      var key = self.keys[i], obj = map[key], obj2 = map2[key];
      if (obj2 === undefined || #{`obj` != `obj2`}) {
        return false;
      }
    }

    return true;
  }
end

#[](key) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'opal/opal/corelib/hash.rb', line 59

def [](key)
  %x{
    var map = self.map;

    if ($opal.hasOwnProperty.call(map, key)) {
      return map[key];
    }

    var proc = #@proc;

    if (proc !== nil) {
      return #{ `proc`.call self, key };
    }

    return #@none;
  }
end

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



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'opal/opal/corelib/hash.rb', line 77

def []=(key, value)
  %x{
    var map = self.map;

    if (!$opal.hasOwnProperty.call(map, key)) {
      self.keys.push(key);
    }

    map[key] = value;

    return value;
  }
end

#assoc(object) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'opal/opal/corelib/hash.rb', line 91

def assoc(object)
  %x{
    var keys = self.keys, key;

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

      if (#{`key` == object}) {
        return [key, self.map[key]];
      }
    }

    return nil;
  }
end

#clearObject



107
108
109
110
111
112
113
# File 'opal/opal/corelib/hash.rb', line 107

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

#cloneObject Also known as: dup



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'opal/opal/corelib/hash.rb', line 115

def clone
  %x{
    var map  = {},
        keys = [];

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

      keys.push(key);
      map[key] = value;
    }

    var hash = new self._klass._alloc();

    hash.map  = map;
    hash.keys = keys;
    hash.none = self.none;
    hash.proc = self.proc;

    return hash;
  }
end

#default(val = undefined) ⇒ Object



139
140
141
142
143
144
145
146
# File 'opal/opal/corelib/hash.rb', line 139

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

#default=(object) ⇒ Object



148
149
150
151
152
153
# File 'opal/opal/corelib/hash.rb', line 148

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

#default_procObject



155
156
157
# File 'opal/opal/corelib/hash.rb', line 155

def default_proc
  @proc
end

#default_proc=(proc) ⇒ Object



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

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



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'opal/opal/corelib/hash.rb', line 173

def delete(key, &block)
  %x{
    var map  = self.map, result = map[key];

    if (result != null) {
      delete map[key];
      self.keys.$delete(key);

      return result;
    }

    if (block !== nil) {
      return #{block.call(key)};
    }
    return nil;
  }
end

#delete_if(&block) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'opal/opal/corelib/hash.rb', line 191

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

  %x{
    var map = self.map, keys = self.keys, value;

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

      if ((value = block(key, obj)) === $breaker) {
        return $breaker.$v;
      }

      if (value !== false && value !== nil) {
        keys.splice(i, 1);
        delete map[key];

        length--;
        i--;
      }
    }

    return self;
  }
end

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



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'opal/opal/corelib/hash.rb', line 219

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

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

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

      if (value === $breaker) {
        return $breaker.$v;
      }
    }

    return self;
  }
end

#each_key(&block) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'opal/opal/corelib/hash.rb', line 239

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

  %x{
    var keys = self.keys;

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

      if (block(key) === $breaker) {
        return $breaker.$v;
      }
    }

    return self;
  }
end

#each_value(&block) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'opal/opal/corelib/hash.rb', line 259

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

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

    for (var i = 0, length = keys.length; i < length; i++) {
      if (block(map[keys[i]]) === $breaker) {
        return $breaker.$v;
      }
    }

    return self;
  }
end

#empty?Boolean

Returns:



275
276
277
# File 'opal/opal/corelib/hash.rb', line 275

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

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



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 281

def fetch(key, defaults = undefined, &block)
  %x{
    var value = self.map[key];

    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



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'opal/opal/corelib/hash.rb', line 307

def flatten(level=undefined)
  %x{
    var map = self.map, keys = self.keys, result = [];

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

      result.push(key);

      if (value._isArray) {
        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:



333
334
335
# File 'opal/opal/corelib/hash.rb', line 333

def has_key?(key)
  `$opal.hasOwnProperty.call(self.map, key)`
end

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

Returns:



337
338
339
340
341
342
343
344
345
346
347
# File 'opal/opal/corelib/hash.rb', line 337

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

    return false;
  }
end

#hashObject



349
350
351
# File 'opal/opal/corelib/hash.rb', line 349

def hash
  `self._id`
end

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



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

def index(object)
  %x{
    var map = self.map, keys = self.keys;

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

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

    return nil;
  }
end

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



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'opal/opal/corelib/hash.rb', line 371

def indexes(*keys)
  %x{
    var result = [], map = self.map, val;

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

      if (val != null) {
        result.push(val);
      }
      else {
        result.push(self.none);
      }
    }

    return result;
  }
end

#inspectObject Also known as: to_s



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'opal/opal/corelib/hash.rb', line 392

def inspect
  %x{
    var inspect = [], keys = self.keys, map = self.map;

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

      if (val === self) {
        inspect.push(#{`key`.inspect} + '=>' + '{...}');
      } else {
        inspect.push(#{`key`.inspect} + '=>' + #{`map[key]`.inspect});
      }
    }

    return '{' + inspect.join(', ') + '}';
  }
end

#invertObject



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'opal/opal/corelib/hash.rb', line 410

def invert
  %x{
    var result = $opal.hash(), keys = self.keys, map = self.map,
        keys2 = result.keys, map2 = result.map;

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

      keys2.push(obj);
      map2[obj] = key;
    }

    return result;
  }
end

#keep_if(&block) ⇒ Object



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'opal/opal/corelib/hash.rb', line 426

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

  %x{
    var map = self.map, keys = self.keys, value;

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

      if ((value = block(key, obj)) === $breaker) {
        return $breaker.$v;
      }

      if (value === false || value === nil) {
        keys.splice(i, 1);
        delete map[key];

        length--;
        i--;
      }
    }

    return self;
  }
end

#keysObject



456
457
458
# File 'opal/opal/corelib/hash.rb', line 456

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

#lengthObject Also known as: size



460
461
462
# File 'opal/opal/corelib/hash.rb', line 460

def length
  `self.keys.length`
end

#merge(other, &block) ⇒ Object



466
467
468
469
470
471
472
473
474
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
# File 'opal/opal/corelib/hash.rb', line 466

def merge(other, &block)
  %x{
    if (! #{Hash === other}) {
      other = #{Opal.coerce_to!(other, Hash, :to_hash)};
    }

    var keys = self.keys, map = self.map,
        result = $opal.hash(), keys2 = result.keys, map2 = result.map;

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

      keys2.push(key);
      map2[key] = map[key];
    }

    var keys = other.keys, map = other.map;

    if (block === nil) {
      for (var i = 0, length = keys.length; i < length; i++) {
        var key = keys[i];

        if (map2[key] == null) {
          keys2.push(key);
        }

        map2[key] = map[key];
      }
    }
    else {
      for (var i = 0, length = keys.length; i < length; i++) {
        var key = keys[i];

        if (map2[key] == null) {
          keys2.push(key);
          map2[key] = map[key];
        }
        else {
          map2[key] = block(key, map2[key], map[key]);
        }
      }
    }

    return result;
  }
end

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



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
544
545
546
547
548
549
# File 'opal/opal/corelib/hash.rb', line 513

def merge!(other, &block)
  %x{
    if (! #{Hash === other}) {
      other = #{Opal.coerce_to!(other, Hash, :to_hash)};
    }

    var keys = self.keys, map = self.map,
        keys2 = other.keys, map2 = other.map;

    if (block === nil) {
      for (var i = 0, length = keys2.length; i < length; i++) {
        var key = keys2[i];

        if (map[key] == null) {
          keys.push(key);
        }

        map[key] = map2[key];
      }
    }
    else {
      for (var i = 0, length = keys2.length; i < length; i++) {
        var key = keys2[i];

        if (map[key] == null) {
          keys.push(key);
          map[key] = map2[key];
        }
        else {
          map[key] = block(key, map[key], map2[key]);
        }
      }
    }

    return self;
  }
end

#rassoc(object) ⇒ Object



551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'opal/opal/corelib/hash.rb', line 551

def rassoc(object)
  %x{
    var keys = self.keys, map = self.map;

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

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

    return nil;
  }
end

#reject(&block) ⇒ Object



567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# File 'opal/opal/corelib/hash.rb', line 567

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

  %x{
    var keys = self.keys, map = self.map,
        result = $opal.hash(), map2 = result.map, keys2 = result.keys;

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

      if ((value = block(key, obj)) === $breaker) {
        return $breaker.$v;
      }

      if (value === false || value === nil) {
        keys2.push(key);
        map2[key] = obj;
      }
    }

    return result;
  }
end

#replace(other) ⇒ Object



591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'opal/opal/corelib/hash.rb', line 591

def replace(other)
  %x{
    var map = self.map = {}, keys = self.keys = [];

    for (var i = 0, length = other.keys.length; i < length; i++) {
      var key = other.keys[i];
      keys.push(key);
      map[key] = other.map[key];
    }

    return self;
  }
end

#select(&block) ⇒ Object



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 605

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

  %x{
    var keys = self.keys, map = self.map,
        result = $opal.hash(), map2 = result.map, keys2 = result.keys;

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

      if ((value = block(key, obj)) === $breaker) {
        return $breaker.$v;
      }

      if (value !== false && value !== nil) {
        keys2.push(key);
        map2[key] = obj;
      }
    }

    return result;
  }
end

#select!(&block) ⇒ Object



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

def select!(&block)
  return enum_for :select! unless block

  %x{
    var map = self.map, keys = self.keys, value, result = nil;

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

      if ((value = block(key, obj)) === $breaker) {
        return $breaker.$v;
      }

      if (value === false || value === nil) {
        keys.splice(i, 1);
        delete map[key];

        length--;
        i--;
        result = self
      }
    }

    return result;
  }
end

#shiftObject



656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
# File 'opal/opal/corelib/hash.rb', line 656

def shift
  %x{
    var keys = self.keys, map = self.map;

    if (keys.length) {
      var key = keys[0], obj = map[key];

      delete map[key];
      keys.splice(0, 1);

      return [key, obj];
    }

    return nil;
  }
end

#to_aObject



677
678
679
680
681
682
683
684
685
686
687
688
# File 'opal/opal/corelib/hash.rb', line 677

def to_a
  %x{
    var keys = self.keys, map = self.map, result = [];

    for (var i = 0, length = keys.length; i < length; i++) {
      var key = keys[i];
      result.push([key, map[key]]);
    }

    return result;
  }
end

#to_hObject



690
691
692
693
694
695
696
697
698
699
700
701
702
# File 'opal/opal/corelib/hash.rb', line 690

def to_h
  %x{
    var hash   = new Opal.Hash._alloc,
        cloned = #{clone};

    hash.map  = cloned.map;
    hash.keys = cloned.keys;
    hash.none = cloned.none;
    hash.proc = cloned.proc;

    return hash;
  }
end

#to_hashObject



704
705
706
# File 'opal/opal/corelib/hash.rb', line 704

def to_hash
  self
end

#valuesObject



716
717
718
719
720
721
722
723
724
725
726
727
# File 'opal/opal/corelib/hash.rb', line 716

def values
  %x{
    var map    = self.map,
        result = [];

    for (var key in map) {
      result.push(map[key]);
    }

    return result;
  }
end