Last active
May 4, 2024 07:15
-
-
Save toomasv/fd3338a500fdf3e6da49b9d18ffb8c70 to your computer and use it in GitHub Desktop.
Transform data into block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
make-block: function [data /all /with selection [word! block!]][ | |
if word? selection [selection: to-block selection] | |
either any [ | |
accessors: select system/catalog/accessors type?/word data | |
system/words/all [ ; NB! Modifies global "query" | |
url? data | |
data: decode-url data | |
accessors: words-of data | |
] | |
system/words/all [ | |
file? data | |
accessors: [path name suffix modified] | |
data: object [ | |
path: | |
name: none | |
set [path name] split-path data | |
suffix: suffix? data | |
modified: system/words/query data | |
] | |
] | |
][ | |
either selection [ | |
if not empty? weird: exclude selection accessors [ | |
cause-error 'user 'message rejoin ["make-block: Unknown accessor" pick [": " "s: "] single? weird weird] | |
] | |
][ | |
selection: accessors | |
] | |
collect [ | |
foreach sel selection [ | |
if all [keep to set-word! sel] | |
keep switch/default type?/word d: data/:sel [ | |
word! [to-lit-word d] | |
path! [to-lit-path d] | |
][d] | |
] | |
] | |
][ | |
if any [all with][ | |
cause-error 'user 'message rejoin ["make-block: Data of type `" mold type? data "` does not have accessors!"] | |
] | |
append copy [] data | |
] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: