Skip to content

Commit

Permalink
Implement maktaba#value#AsList helper to coerce values to a list (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarnett authored Mar 21, 2023
1 parent 5d416e8 commit fe95bb1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
13 changes: 13 additions & 0 deletions autoload/maktaba/value.vim
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,19 @@ function! maktaba#value#IsEnum(Value) abort
endfunction


""
" Coerces {value_or_values} into a list, wrapping any non-list value into a
" single-item list.
" Examples: >
" maktaba#value#AsList('lonely') == ['lonely']
" maktaba#value#AsList(['pb', 'jelly']) == ['pb', 'jelly']
" <
function! maktaba#value#AsList(Value_or_values) abort
return maktaba#value#IsList(a:Value_or_values) ?
\ a:Value_or_values : [a:Value_or_values]
endfunction


""
" Focuses on a part of {target} specified by {foci}. That object will either be
" returned, or set to [value] if [value] is given (in which case {target} is
Expand Down
8 changes: 8 additions & 0 deletions doc/maktaba.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1898,6 +1898,14 @@ maktaba#value#IsCallable({value}) *maktaba#value#IsCallable()*
maktaba#value#IsEnum({value}) *maktaba#value#IsEnum()*
1 if {value} is a maktaba enum type, 0 otherwise.

maktaba#value#AsList({value_or_values}) *maktaba#value#AsList()*
Coerces {value_or_values} into a list, wrapping any non-list value into a
single-item list. Examples:
>
maktaba#value#AsList('lonely') == ['lonely']
maktaba#value#AsList(['pb', 'jelly']) == ['pb', 'jelly']
<

maktaba#value#Focus({target}, {foci}, [value]) *maktaba#value#Focus()*
Focuses on a part of {target} specified by {foci}. That object will either
be returned, or set to [value] if [value] is given (in which case {target}
Expand Down
13 changes: 13 additions & 0 deletions vroom/value.vroom
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,19 @@ The empty funcref value is function('empty').

Cute.

You can also coerce an arbitrary value into a list using maktaba#value#AsList
(wrapping non-list values into their own single-item list).

:echomsg string(maktaba#value#AsList('lonely'))
~ ['lonely']
:echomsg string(maktaba#value#AsList({'abc': 123}))
~ [{'abc': 123}]

If the value is already a list, it's returned unchanged.

:echomsg string(maktaba#value#AsList(['pb', 'jelly']))
~ ['pb', 'jelly']



Sometimes you want to check whether a variable can be called, or whether
Expand Down

0 comments on commit fe95bb1

Please sign in to comment.