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



21
22
23
24
25
26
27
28
29
30
31
32
# File 'opal/opal/corelib/hash.rb', line 21

def initialize(defaults = undefined, &block)
  %x{
    if (defaults != null) {
      self.none = defaults;
    }
    else if (block !== nil) {
      self.proc = block;
    }

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

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

    hash.map  = {};
    hash.keys = [];

    return hash;
  }
end

Instance Method Details

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



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

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 (#{`obj` != `obj2`}) {
        return false;
      }
    }

    return true;
  }
end

#[](key) ⇒ Object



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

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

    if ($hasOwn.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



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'opal/opal/corelib/hash.rb', line 81

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

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

    map[key] = value;

    return value;
  }
end

#assoc(object) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'opal/opal/corelib/hash.rb', line 95

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



111
112
113
114
115
116
117
# File 'opal/opal/corelib/hash.rb', line 111

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

#cloneObject Also known as: dup



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'opal/opal/corelib/hash.rb', line 119

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



143
144
145
# File 'opal/opal/corelib/hash.rb', line 143

def default(val = undefined)
  @none
end

#default=(object) ⇒ Object



147
148
149
# File 'opal/opal/corelib/hash.rb', line 147

def default=(object)
  @none = object
end

#default_procObject



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

def default_proc
  @proc
end

#default_proc=(proc) ⇒ Object



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

def default_proc=(proc)
  @proc = proc
end

#delete(key) ⇒ Object



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

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

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

      return result;
    }

    return nil;
  }
end

#delete_if(&block) ⇒ Object



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

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



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

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



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

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



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

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:



258
259
260
# File 'opal/opal/corelib/hash.rb', line 258

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

#fetch(key, defaults = undefined, &block) ⇒ 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
# File 'opal/opal/corelib/hash.rb', line 264

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



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

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:



316
317
318
# File 'opal/opal/corelib/hash.rb', line 316

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

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

Returns:



320
321
322
323
324
325
326
327
328
329
330
# File 'opal/opal/corelib/hash.rb', line 320

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

    return false;
  }
end

#hashObject



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

def hash
  `self._id`
end

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



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

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



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

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



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

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



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

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



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'opal/opal/corelib/hash.rb', line 409

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



439
440
441
# File 'opal/opal/corelib/hash.rb', line 439

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

#lengthObject Also known as: size



443
444
445
# File 'opal/opal/corelib/hash.rb', line 443

def length
  `self.keys.length`
end

#merge(other, &block) ⇒ Object



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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'opal/opal/corelib/hash.rb', line 449

def merge(other, &block)
  %x{
    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



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'opal/opal/corelib/hash.rb', line 492

def merge!(other, &block)
  %x{
    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



526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'opal/opal/corelib/hash.rb', line 526

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



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 542

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



566
567
568
569
570
571
572
573
574
575
576
577
578
# File 'opal/opal/corelib/hash.rb', line 566

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



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 580

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



604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
# File 'opal/opal/corelib/hash.rb', line 604

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



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

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



652
653
654
655
656
657
658
659
660
661
662
663
# File 'opal/opal/corelib/hash.rb', line 652

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



665
666
667
668
669
670
671
672
673
674
675
676
677
# File 'opal/opal/corelib/hash.rb', line 665

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



679
680
681
# File 'opal/opal/corelib/hash.rb', line 679

def to_hash
  self
end

#valuesObject



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

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

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

    return result;
  }
end