barefoot_js 0.40.0 → 0.40.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/barefoot_js.rb +41 -2
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5683e188c57d0ed7b6101db57828d8bfb0a6f8ffdbb6939052e5132fbd099fd9
4
- data.tar.gz: 546f0e92374f3947eae8965b2771a3ba6928526eddaa8b9f1e3ced6883deed1f
3
+ metadata.gz: 949fd035bff68603576788c43d9f94e354632d2c9e23ced564a5a22c83d901d4
4
+ data.tar.gz: b01b798e43b952abbe9728e4bfdeeb58922dd93718ded181d649e79233b3c472
5
5
  SHA512:
6
- metadata.gz: 99c0e7ceaf4e6e92c1a0b9c462a43983b91de28555e6032c11e0b904156088e7ba08177043f3a3658140a18de3d1cae365746c01193d87b5c83e5db6cbf21a1a
7
- data.tar.gz: dc2aa7213b1e63646a13f0c9340377731ab62baf7731ffcbed0bb517a78ff79d866f08a3a7c985990f666f01532a79237be42c346791d7bff213fa1e83f3a5c6
6
+ metadata.gz: 6a93a544672bd9897f2a3c995ccb15490c1c932837c088f1b957198dd671f35137c6f79fc6ffb72aed8001c787d992021573000cc922b0a5d36d801337a83344
7
+ data.tar.gz: 377c648e844b16fe943e8062fd004e8c06b6e8b2e8fcc019b444657fb09d49a0634ac1ac9a6e8fe08746b0a670e93c1f586b2de857df9683f5a3f012287a92f3
data/lib/barefoot_js.rb CHANGED
@@ -330,7 +330,32 @@ module BarefootJS
330
330
  # `props[:children]` as already-rendered HTML. Guard on `key?` so a
331
331
  # childless invocation doesn't gain a spurious `children: nil` key.
332
332
  props[:children] = backend.materialize(props[:children]) if props.key?(:children)
333
- renderer.call(props, self)
333
+ # Record the keys this call passes for the child's `passed?` while its
334
+ # template renders. The renderer builds the child's own Context, so
335
+ # the record rides on a per-thread stack rather than on that object;
336
+ # rendering is synchronous, so its top is always the keys of the
337
+ # component whose template is running.
338
+ stack = (Thread.current[:barefoot_js_caller_keys] ||= [])
339
+ stack.push(props.keys.map(&:to_s))
340
+ begin
341
+ renderer.call(props, self)
342
+ ensure
343
+ stack.pop
344
+ end
345
+ end
346
+
347
+ # Whether this component's caller passed the prop `name`, even as `nil`:
348
+ # JS's `name in props`, which a template can't ask of its vars (every
349
+ # declared prop is seeded, so an omitted one and a `nil` one read the
350
+ # same). A `{...props}` spread onto a child carries a param only when
351
+ # this is true. The keys are those `render_child` recorded for a child
352
+ # and `_props` for the root; with neither recorded (a root rendered
353
+ # without `_props`), `fallback`, the template's own "not nil" test,
354
+ # decides.
355
+ def passed?(name, fallback)
356
+ stack = Thread.current[:barefoot_js_caller_keys]
357
+ keys = stack && !stack.empty? ? stack.last : _props&.keys&.map(&:to_s)
358
+ keys ? keys.include?(name.to_s) : fallback
334
359
  end
335
360
 
336
361
  # -----------------------------------------------------------------
@@ -485,8 +510,22 @@ module BarefootJS
485
510
  NUMERIC_STRING_RE = /\A\s*[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?\s*\z/.freeze
486
511
 
487
512
  def json(value)
488
- backend.encode_json(value)
513
+ backend.encode_json(json_unsign_zeros(value))
514
+ end
515
+
516
+ # JS `JSON.stringify` writes -0 as `0` at any depth (`[-4 % 2, 1]` ->
517
+ # `[0,1]`), but Ruby's encoder spells a Float zero `-0.0` / `0.0` --
518
+ # the `-0.0` that `Evaluator.remainder` gives for a negative dividend.
519
+ # Float zeros become Integer 0, inside Arrays and Hash values too.
520
+ def json_unsign_zeros(value)
521
+ case value
522
+ when Float then value.zero? ? 0 : value
523
+ when Array then value.map { |el| json_unsign_zeros(el) }
524
+ when Hash then value.transform_values { |v| json_unsign_zeros(v) }
525
+ else value
526
+ end
489
527
  end
528
+ private :json_unsign_zeros
490
529
 
491
530
  # JS `String(v)` mirror, EXCEPT `nil` renders as '' (not the literal
492
531
  # "null") so an unset prop doesn't surface as literal text in
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: barefoot_js
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.40.0
4
+ version: 0.40.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - kobaken