Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixes Issue #11: how to delete a property from an object?

Adds a runtime directive of "delete()" as recommended by @coolaj86.
The parsing has been redone slightly to be more accommodating to
distinguish between variables and member functions.
  • Loading branch information...
commit 7537095b6508b824bc60e03a7f81df30ca3570ce 1 parent bcf8383
@kristopolous kristopolous authored
Showing with 19 additions and 7 deletions.
  1. +3 −0  README.md
  2. +9 −0 tests/delete.sh
  3. +7 −7 ticktick.sh
View
3  README.md
@@ -17,8 +17,11 @@ A few array manipulation runtime directives are supported:
* `push` <pre>\`\`arr.push(${arr_len})\`\`</pre>
* `pop` <pre>echo \`\`arr.pop()\`\`</pre>
* `shift` <pre>echo \`\`arr.shift()\`\`</pre>
+ * `deletet` <pre>echo \`\`key.value.delete()\`\`</pre>
* `items` <pre>for x in \`\`arr.items()\`\`; do echo "${x}"; done</pre>
+Note that this doesn't preclude having variables by those names. You can have ``key.delete = 1`` and then ``key.delete.delete()``
+
Objects
---
View
9 tests/delete.sh
@@ -0,0 +1,9 @@
+#!/bin/bash
+. ../ticktick.sh
+
+`` key = {"value": 1} ``
+
+echo ``key.value``
+``key.value.delete()``
+echo ``key.value``
+
View
14 ticktick.sh
@@ -100,7 +100,7 @@ __tick_json_parse() {
__tick_fun_tokenize_expression() {
CHAR='[A-Za-z_$\\]'
- FUNCTION="(push|pop|shift|items|length)"
+ FUNCTION="(push|pop|shift|items|delete|length)[[:space:]]*\("
NUMBER='[0-9]*'
STRING="$CHAR*($CHAR*)*"
PAREN="[()]"
@@ -111,8 +111,6 @@ __tick_fun_tokenize_expression() {
}
__tick_fun_parse_expression() {
- local paren=0
-
while read -r token; do
token=${token/#S/}
token=${token/%E/}
@@ -121,16 +119,18 @@ __tick_fun_parse_expression() {
suffix+="$token"
else
case "$token" in
- push|pop|shift|items|length) function=$token ;;
- '(') (( paren++ )) ;;
+ 'push('|'pop('|'shift('|'items('|'delete('|'length(') function=$token ;;
')')
+ function=${function/%(/}
case $function in
items) echo '${!__tick_data_'"$Prefix"'*}' ;;
+ delete) echo 'unset __tick_data_'${Prefix/%_/} ;;
pop) echo '"$( __tick_runtime_last ${!__tick_data_'"$Prefix"'*} )"; __tick_runtime_pop ${!__tick_data_'"$Prefix"'*}' ;;
shift) echo '`__tick_runtime_first ${!__tick_data_'"$Prefix"'*}`; __tick_runtime_shift ${!__tick_data_'"$Prefix"'*}' ;;
length) echo '`__tick_runtime_length ${!__tick_data_'"$Prefix"'*}`' ;;
*) echo "__tick_runtime_$function \"$arguments\" __tick_data_$Prefix "'${!__tick_data_'"$Prefix"'*}'
esac
+ unset function
return
;;
@@ -140,8 +140,8 @@ __tick_fun_parse_expression() {
'"'|"'"|']') ;;
=) done=1 ;;
# Only respect a space if its in the args.
- ' ') [ $paren -gt 0 ] && arguments+="$token" ;;
- *) [ $paren -gt 0 ] && arguments+="$token" || Prefix+="$token" ;;
+ ' ') [ -n "$function" ] && arguments+="$token" ;;
+ *) [ -n "$function" ] && arguments+="$token" || Prefix+="$token" ;;
esac
fi
done
Please sign in to comment.
Something went wrong with that request. Please try again.