|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using ReactUnity.Helpers; |
| 6 | + |
| 7 | +namespace ReactUnity.Scripting.DomProxies |
| 8 | +{ |
| 9 | + // https://url.spec.whatwg.org/#urlsearchparams |
| 10 | + public class URLSearchParams: IEnumerable<string[]> |
| 11 | + { |
| 12 | + #region Fields |
| 13 | + |
| 14 | + private readonly List<KeyValuePair<String, String>> _values; |
| 15 | + private readonly URL _parent; |
| 16 | + |
| 17 | + #endregion |
| 18 | + |
| 19 | + #region Constructors |
| 20 | + |
| 21 | + public URLSearchParams() => _values = new List<KeyValuePair<String, String>>(); |
| 22 | + |
| 23 | + internal URLSearchParams(URL parent) : this() |
| 24 | + { |
| 25 | + _parent = parent; |
| 26 | + ChangeTo(parent.search, true); |
| 27 | + } |
| 28 | + |
| 29 | + public URLSearchParams(String init) : this() => ChangeTo(init, false); |
| 30 | + |
| 31 | + #endregion |
| 32 | + |
| 33 | + #region Methods |
| 34 | + |
| 35 | + internal void Reset() => _values.Clear(); |
| 36 | + |
| 37 | + internal void ChangeTo(String query, Boolean fromParent) |
| 38 | + { |
| 39 | + Reset(); |
| 40 | + |
| 41 | + if (query is "") return; |
| 42 | + if (query.StartsWith("?")) query = query.Substring(1); |
| 43 | + |
| 44 | + foreach (var pair in query.Split('&')) |
| 45 | + { |
| 46 | + var kvp = pair.Split('='); |
| 47 | + |
| 48 | + if (kvp.Length > 1) |
| 49 | + { |
| 50 | + AppendCore(Decode(kvp[0]), Decode(kvp[1])); |
| 51 | + } |
| 52 | + else |
| 53 | + { |
| 54 | + AppendCore(Decode(pair), String.Empty); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + RaiseChanged(fromParent); |
| 59 | + } |
| 60 | + |
| 61 | + public void append(String name, String value) |
| 62 | + { |
| 63 | + AppendCore(name, value); |
| 64 | + RaiseChanged(false); |
| 65 | + } |
| 66 | + |
| 67 | + private void AppendCore(String name, String value) |
| 68 | + { |
| 69 | + _values.Add(new KeyValuePair<String, String>(name, value)); |
| 70 | + } |
| 71 | + |
| 72 | + public void delete(String name) |
| 73 | + { |
| 74 | + DeleteCore(name); |
| 75 | + RaiseChanged(false); |
| 76 | + } |
| 77 | + |
| 78 | + private void DeleteCore(String name) |
| 79 | + { |
| 80 | + _values.RemoveAll(p => p.Key == name); |
| 81 | + } |
| 82 | + |
| 83 | + public String get(String name) => _values.Find(p => p.Key == name).Value; |
| 84 | + |
| 85 | + public String[] getAll(String name) => _values.FindAll(p => p.Key == name).Select(m => m.Value).ToArray(); |
| 86 | + |
| 87 | + public Boolean has(String name) => _values.Any(p => p.Key == name); |
| 88 | + |
| 89 | + public void set(String name, String value) |
| 90 | + { |
| 91 | + if (has(name)) |
| 92 | + { |
| 93 | + var index = _values.FindIndex(p => p.Key == name); |
| 94 | + DeleteCore(name); |
| 95 | + _values.Insert(index, new KeyValuePair<String, String>(name, value)); |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + AppendCore(name, value); |
| 100 | + } |
| 101 | + |
| 102 | + RaiseChanged(false); |
| 103 | + } |
| 104 | + |
| 105 | + public void sort() |
| 106 | + { |
| 107 | + _values.Sort((a, b) => a.Key.CompareTo(b.Key)); |
| 108 | + RaiseChanged(false); |
| 109 | + } |
| 110 | + |
| 111 | + public string[] keys() => _values.Select(p => p.Key).Distinct().ToArray(); |
| 112 | + public string[] values() => _values.Select(p => p.Value).ToArray(); |
| 113 | + public string[][] entries() => _values.Select(p => new[] { p.Key, p.Value }).ToArray(); |
| 114 | + public IEnumerator<string[]> GetEnumerator() => ((IEnumerable<string[]>) entries()).GetEnumerator(); |
| 115 | + IEnumerator IEnumerable.GetEnumerator() => entries().GetEnumerator(); |
| 116 | + |
| 117 | + public override String ToString() => String.Join("&", _values.Select(p => $"{Encode(p.Key)}={Encode(p.Value)}")); |
| 118 | + |
| 119 | + #endregion |
| 120 | + |
| 121 | + #region Helpers |
| 122 | + |
| 123 | + private static String Encode(String value) |
| 124 | + { |
| 125 | + return EncodingHelpers.encodeURIComponent(value); |
| 126 | + } |
| 127 | + |
| 128 | + private static String Decode(String value) |
| 129 | + { |
| 130 | + return EncodingHelpers.decodeURIComponent(value); |
| 131 | + } |
| 132 | + |
| 133 | + private void RaiseChanged(Boolean fromParent) |
| 134 | + { |
| 135 | + if (!fromParent && _parent != null) _parent.search = ToString(); |
| 136 | + } |
| 137 | + |
| 138 | + #endregion |
| 139 | + } |
| 140 | +} |
0 commit comments