elm_history_tools 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 280b6456a5473cc4af85ebe96dd303466bf1341d
4
- data.tar.gz: d18f4ff0a50be7ddd539b0198b51d05415812151
2
+ SHA256:
3
+ metadata.gz: 040e25befa828f739530f1dcbe81ba5350cae4b6c06f25fc5a7b5a66da906aed
4
+ data.tar.gz: ba6f918efeacfd835dde3d7fd350c7933bea913a6b6bc7518784abb15e84d35b
5
5
  SHA512:
6
- metadata.gz: b9e621128a84df9e1ee1c84369cff2bb46c6fe7800a664b40ac3f64dc58efa028b140229f2b20693ab7390e3e3e02c319d980416316c0acc0f832c52283a460e
7
- data.tar.gz: 9e7c57afa830d958f2fcce36d9894227605d4afb810a32f6ff679211068327e4c29cb37dc99e60651e8139bacaeadd8912bf1655f316fc7e5c37bc5a8fc3bf75
6
+ metadata.gz: 8eace2f01d5e3af2940fa086b57723a33e54c0a2e6f58499186511d0dd9809cf348d248a9aa6d13f219d5bc4fe37bad355c5d3555bdeeb9e8b47b938f096e67c
7
+ data.tar.gz: 8d503d9254bf9258878591c12e3e59fa5f06bf26d97a68eff5b67fc222b1d9e23dab5eaa85bd77ad11f1bdbcf4869da2736345f95cfec2da0e1d64977253395e
data/README.md CHANGED
@@ -100,89 +100,8 @@ You can then easily loop over this data in Ruby to present a readable internal d
100
100
 
101
101
  ## Elm History Export Format
102
102
 
103
- The Elm 0.18.0 history export is structured as followed:
104
-
105
- ```js
106
- {
107
- "metadata": {
108
- "versions": {"elm": "0.18.0"},
109
- "types": {
110
- // the Message type used by your program
111
- "message": "Message.Message",
112
- // all the type aliases defined in your program
113
- "aliases": {
114
- "Json.Decode.Value": {"args":[],"type":"Json.Encode.Value"},
115
- // etc.
116
- },
117
- // all the union types used in your program
118
- "unions": {
119
- "Maybe.Maybe": {
120
- // what arguments the union type takes
121
- "args": ["a"],
122
- // what tags/constructors make up that union type and what arguments they take
123
- "tags":{
124
- "Just": ["a"],
125
- "Nothing": []
126
- }
127
- }
128
- }
129
- }
130
- }
131
- // what's happened in user session being exported
132
- "history": [
133
- // each entry is stored with the contructor and any ordered arguments passed to it
134
- {"ctor": "MessageType", "_0": "Arg1", "_02": {"ctor": "AnotherType"}},
135
- {"ctor": "AnotherMessageType", "_0": "AnotherArg"},
136
- // etc.
137
- ]
138
- }
139
- ```
140
-
141
- Each entry in the history hash represents an Elm message object -- so
142
-
143
- ```js
144
- {"ctor": "MessageType", "_0": "Arg1", "_02": {"ctor": "AnotherType"}},
145
- ```
146
-
147
- represents the Elm message
148
-
149
- ```elm
150
- -- if type alias SomeType = AnotherType | SomethingElse
151
- --
152
- -- MessageType String SomeType
153
- MessageType "Arg1" AnotherType
154
- ```
155
-
156
- A few notes:
157
-
158
- **Lists**
159
-
160
- List entries are recursively nested objects whose constructor is `::` (cons).
161
-
162
- As an example, an Elm list of three books (`[{title = "Too Like the Lightning"}, {title = "The Fear of Barbarians"}, {title = "Evicted"}]`) would be represented as:
163
-
164
- ```js
165
- {
166
- "ctor": "::",
167
- "_0": {"title": "Too Like the Lightning"},
168
- "_1": {
169
- "ctor": "::",
170
- "_0": {"title": "The Fear of Barbarians"},
171
- "_1": {
172
- "ctor": "::",
173
- "_0": {"title": "Evicted"}
174
- }
175
- }
176
- }
177
- ```
178
-
179
- This is because in Elm, `List` is implemented as a linked list (each element is an object that both stores its value and points to the next element in the list, rather than sitting in an array of plain values).
180
-
181
- You don't need to know anything about linked lists to use Elm (or Javascript or Ruby or, likely, whatever you're using for work or fun -- I've literally never used them in my career), but if you're curious, you can read more about them [on Wikipedia](https://en.wikipedia.org/wiki/Linked_list). You can also check out [this interesting discussion](https://github.com/elm-lang/elm-plans/issues/13) of Arrays vs. Lists in Elm.
182
-
183
- **In the future**
184
-
185
- It looks like the terms will change somewhat in a future version of Elm: `"ctor"` and `"_01"`, `"_02"`, etc. will be [replaced with `$`, `a`, `b`, etc](https://github.com/elm-lang/virtual-dom/commit/61cf2090ecb745542532dd7ea87de37c6ed6c3b4#diff-25d902c24283ab8cfbac54dfa101ad31). ElmHistoryTools will support both formats in the future; should the structure change, obviously that will be addressed too.
103
+ For more information on the Elm history export format, see [this blog
104
+ post](https://medium.com/@arsduo/understanding-an-elm-0-19-history-export-1bca38613840).
186
105
 
187
106
  **Why hashes?**
188
107
 
@@ -2,35 +2,35 @@ module ElmHistoryTools::HistoryFormatter
2
2
  # Given a raw Elm history file parsed to JSON, return a simplified hash of the history.
3
3
  #
4
4
  # For instance, given an array like:
5
- # [{"ctor": "MessageType", "_0": "Arg1", "_02": {"ctor": "AnotherType"}}]
5
+ # [{"$": "MessageType", "a": "Arg1", "a2": {"$": "AnotherType"}}]
6
6
  #
7
7
  # you'll get
8
8
  #
9
9
  # [{"MessageType" => ["Arg1", {"AnotherType" => []}]}]
10
10
  #
11
11
  def self.to_simple_hash(history_data)
12
- history_data["history"].map do |entry|
12
+ history_data.dig("a", "history").map do |entry|
13
13
  simplify_history_entry(entry)
14
14
  end
15
15
  end
16
16
 
17
17
  # Turn an Elm history entry into a simple Ruby hash, as described above.
18
18
  #
19
- # Constructors that take no arguments are represented as taking an empty list (see above); an
19
+ # Constru$s that take no arguments are represented as taking an empty list (see above); an
20
20
  # alternative approach would be to use nil. While that would clearly distinguish between those
21
21
  # cases, it would make working with the results more complicated.
22
22
  def self.simplify_history_entry(entry)
23
23
  ElmHistoryTools::Utils.transform_object(entry) do |object_hash|
24
- if object_hash["ctor"] == "::"
25
- # Elm lists are represented as nested entries with the contructor ::. (See the readme for
24
+ if object_hash["$"] == "::"
25
+ # Elm lists are represented as nested entries with the contru$ ::. (See the readme for
26
26
  # more detail.)
27
27
  # We collapse those into a proper Ruby array via flatten.
28
28
  # The last entry of the list will have no nested entry, so we use compact to remove the nil.
29
- [simplify_history_entry(object_hash["_0"]), simplify_history_entry(object_hash["_1"])].compact.flatten
30
- elsif object_hash["ctor"]
29
+ [simplify_history_entry(object_hash["a"]), simplify_history_entry(object_hash["b"])].compact.flatten
30
+ elsif object_hash["$"]
31
31
  # we have an Elm object type (we know this because non-objects aren't passed to the block)
32
32
  {
33
- object_hash["ctor"] => object_hash.reject {|k, _v| k == "ctor"}.values.map {|val| simplify_history_entry(val) }
33
+ object_hash["$"] => object_hash.reject {|k, _v| k == "$"}.values.map {|val| simplify_history_entry(val) }
34
34
  }
35
35
  end
36
36
  end
@@ -20,21 +20,23 @@ module ElmHistoryTools::HistorySanitizer
20
20
  # further.
21
21
  def self.sanitize_history(history_data, watch_words, &sanitizing_block)
22
22
  matchers = watch_words.map {|word| word.is_a?(Regexp) ? word : Regexp.new(word, true)}
23
- history_data.dup.merge(
24
- "history" => history_data["history"].map {|entry| sanitize(entry, matchers, &sanitizing_block)}
23
+ data = history_data.dup
24
+ data["a"].merge!(
25
+ "history" => data["a"]["history"].map {|entry| sanitize(entry, matchers, &sanitizing_block)}
25
26
  )
27
+ data
26
28
  end
27
29
 
28
30
  # For each entry, we sanitize it if there's a matching handler. If there is no sanitizer, see if
29
31
  # any data in the entry merit flagging against a watch word.
30
32
  def self.sanitize(entry, matchers, &sanitizing_block)
31
33
  ElmHistoryTools::Utils.transform_object(entry) do |elm_object|
32
- constructor = elm_object["ctor"]
34
+ constructor = elm_object["$"]
33
35
  raise ElmObjectExpected unless constructor
34
36
 
35
37
  # replace any records that need it
36
38
  cleaned_object = elm_object.each_with_object({}) do |(key, value), hash|
37
- if value.is_a?(Hash) && value["ctor"]
39
+ if value.is_a?(Hash) && value["$"]
38
40
  # we have an elm object -- sanitize that
39
41
  hash[key] = sanitize(value, matchers, &sanitizing_block)
40
42
  elsif value.is_a?(Hash)
@@ -9,7 +9,7 @@ module ElmHistoryTools::Utils
9
9
  def self.transform_object(entry, &block)
10
10
  if !entry.is_a?(Hash)
11
11
  entry
12
- elsif entry["ctor"]
12
+ elsif entry["$"]
13
13
  # If you want to walk down sub-entries, call transform_object within your block.
14
14
  yield entry
15
15
  else
@@ -1,3 +1,3 @@
1
1
  module ElmHistoryTools
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elm_history_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Koppel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-05 00:00:00.000000000 Z
11
+ date: 2019-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -81,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  version: '0'
82
82
  requirements: []
83
83
  rubyforge_project:
84
- rubygems_version: 2.6.12
84
+ rubygems_version: 2.7.7
85
85
  signing_key:
86
86
  specification_version: 4
87
87
  summary: Tools to work with Elm history exports.