Class: Dommy::URLSearchParams
- Inherits:
-
Object
- Object
- Dommy::URLSearchParams
- Includes:
- Bridge::Methods, Enumerable
- Defined in:
- lib/dommy/url.rb
Instance Method Summary collapse
- #__internal_replace__(query_string) ⇒ Object
- #__js_call__(method, args) ⇒ Object
- #__js_get__(key) ⇒ Object
- #append(name, value) ⇒ Object
-
#delete(name, value = UNSET) ⇒ Object
WHATWG delete(name) / delete(name, value): with a value, only removes pairs whose value also matches.
- #each(&block) ⇒ Object
- #entries ⇒ Object
- #for_each(&block) ⇒ Object (also: #forEach)
- #get(name) ⇒ Object
- #get_all(name) ⇒ Object (also: #getAll)
-
#has(name, value = UNSET) ⇒ Object
(also: #has?)
WHATWG has(name) / has(name, value): with a value, only matches a pair whose value also equals it.
-
#initialize(input = "", owner: nil) ⇒ URLSearchParams
constructor
A new instance of URLSearchParams.
- #keys ⇒ Object
- #set(name, value) ⇒ Object
- #size ⇒ Object (also: #length)
-
#sort ⇒ Object
WHATWG: sort by comparison of the names’ UTF-16 *code units* (not code points — so a surrogate-pair character sorts by its leading 0xD800–0xDBFF unit), preserving the relative order of pairs with equal names (Ruby’s sort_by is not stable, hence the index tiebreak).
- #to_s ⇒ Object
- #values ⇒ Object
Methods included from Bridge::Methods
Constructor Details
#initialize(input = "", owner: nil) ⇒ URLSearchParams
Returns a new instance of URLSearchParams.
336 337 338 339 |
# File 'lib/dommy/url.rb', line 336 def initialize(input = "", owner: nil) @owner = owner @pairs = parse(input) end |
Instance Method Details
#__internal_replace__(query_string) ⇒ Object
455 456 457 458 |
# File 'lib/dommy/url.rb', line 455 def __internal_replace__(query_string) @pairs = parse(query_string) nil end |
#__js_call__(method, args) ⇒ Object
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 |
# File 'lib/dommy/url.rb', line 469 def __js_call__(method, args) case method when "get" get(args[0]) when "getAll" get_all(args[0]) when "has" value_given?(args) ? has(args[0], args[1]) : has(args[0]) when "set" set(args[0], args[1]) when "append" append(args[0], args[1]) when "delete" value_given?(args) ? delete(args[0], args[1]) : delete(args[0]) when "sort" sort when "toString" to_s when "forEach" # The callback is a live JS function (HostCallback), not a Ruby Proc, so # invoke it through the bridge ABI rather than `&block` (which would try # to to_proc it). callback(value, key, this) per WHATWG. cb = args[0] @pairs.each do |k, v| if cb.respond_to?(:__js_call__) cb.__js_call__("call", [v, k, self]) elsif cb.respond_to?(:call) cb.call(v, k, self) end end nil when "keys" keys when "values" values when "entries" entries end end |
#__js_get__(key) ⇒ Object
460 461 462 463 464 465 |
# File 'lib/dommy/url.rb', line 460 def __js_get__(key) case key when "size", "length" size end end |
#append(name, value) ⇒ Object
387 388 389 390 391 |
# File 'lib/dommy/url.rb', line 387 def append(name, value) @pairs << [stringify(name), stringify(value)] notify nil end |
#delete(name, value = UNSET) ⇒ Object
WHATWG delete(name) / delete(name, value): with a value, only removes pairs whose value also matches.
395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/dommy/url.rb', line 395 def delete(name, value = UNSET) key = stringify(name) if UNSET.equal?(value) @pairs.reject! { |k, _| k == key } else val = stringify(value) @pairs.reject! { |k, vv| k == key && vv == val } end notify nil end |
#each(&block) ⇒ Object
428 429 430 |
# File 'lib/dommy/url.rb', line 428 def each(&block) @pairs.each(&block) end |
#entries ⇒ Object
440 441 442 |
# File 'lib/dommy/url.rb', line 440 def entries @pairs.dup end |
#for_each(&block) ⇒ Object Also known as: forEach
444 445 446 447 |
# File 'lib/dommy/url.rb', line 444 def for_each(&block) @pairs.each { |k, v| block.call(v, k, self) } nil end |
#get(name) ⇒ Object
341 342 343 344 345 |
# File 'lib/dommy/url.rb', line 341 def get(name) key = stringify(name) pair = @pairs.find { |k, _| k == key } pair && pair[1] end |
#get_all(name) ⇒ Object Also known as: getAll
347 348 349 350 |
# File 'lib/dommy/url.rb', line 347 def get_all(name) key = stringify(name) @pairs.select { |k, _| k == key }.map { |_, v| v } end |
#has(name, value = UNSET) ⇒ Object Also known as: has?
WHATWG has(name) / has(name, value): with a value, only matches a pair whose value also equals it.
356 357 358 359 360 361 362 |
# File 'lib/dommy/url.rb', line 356 def has(name, value = UNSET) key = stringify(name) return @pairs.any? { |k, _| k == key } if UNSET.equal?(value) val = stringify(value) @pairs.any? { |k, v| k == key && v == val } end |
#keys ⇒ Object
432 433 434 |
# File 'lib/dommy/url.rb', line 432 def keys @pairs.map { |k, _| k } end |
#set(name, value) ⇒ Object
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/dommy/url.rb', line 366 def set(name, value) key = stringify(name) first_done = false @pairs = @pairs.reject do |k, _| next false unless k == key if first_done true else first_done = true false end end val = stringify(value) @pairs.map! { |pair| pair[0] == key ? [key, val] : pair } @pairs << [key, val] unless first_done notify nil end |
#size ⇒ Object Also known as: length
422 423 424 |
# File 'lib/dommy/url.rb', line 422 def size @pairs.length end |
#sort ⇒ Object
WHATWG: sort by comparison of the names’ UTF-16 *code units* (not code points — so a surrogate-pair character sorts by its leading 0xD800–0xDBFF unit), preserving the relative order of pairs with equal names (Ruby’s sort_by is not stable, hence the index tiebreak).
413 414 415 416 417 418 419 420 |
# File 'lib/dommy/url.rb', line 413 def sort @pairs = @pairs .each_with_index .sort_by { |(name, _value), idx| [name.encode(Encoding::UTF_16BE).unpack("n*"), idx] } .map(&:first) notify nil end |
#to_s ⇒ Object
451 452 453 |
# File 'lib/dommy/url.rb', line 451 def to_s @pairs.map { |k, v| "#{encode(k)}=#{encode(v)}" }.join("&") end |
#values ⇒ Object
436 437 438 |
# File 'lib/dommy/url.rb', line 436 def values @pairs.map { |_, v| v } end |