From 8b8c7e85336d3fa3ec08c34ea69356d97502121d Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Mon, 20 Dec 2010 22:19:18 -0500 Subject: [PATCH] separated rmtrail function into its own tool and added test --- test/test-rmtrail | 54 +++++++++++++++++++++++++++++++++++++++++++++++ tools/combine | 35 ++---------------------------- tools/rmtrail | 51 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 33 deletions(-) create mode 100755 test/test-rmtrail create mode 100755 tools/rmtrail diff --git a/test/test-rmtrail b/test/test-rmtrail new file mode 100755 index 0000000..d02c609 --- /dev/null +++ b/test/test-rmtrail @@ -0,0 +1,54 @@ +#!/bin/bash +# +# Copyright (C) 2010 Mike Gerwitz +# +# This file is part of ease.js. +# +# ease.js is free software: you can redistribute it and/or modify it under the +# terms of the GNU Lesser General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License +# for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see . +# # + +PATH_TOOLS=$( dirname "$0" )/../tools + +# string to remove trailing commas from +TEST_DATA=$( echo '[a,b,]{a,b,}[ + a, + b, + ]{ + a, + b, + }' \ + | $PATH_TOOLS/rmtrail +) + +# expected result +TEST_CMP='[a,b]{a,b}[ + a, + b + ]{ + a, + b + }' + +# perform the assertion +if [ ! "$TEST_DATA" == "$TEST_CMP" ]; then + echo "rmtrail failed" + echo "< expected" + echo "> given" + + diff <( echo "$TEST_CMP" ) <( echo "$TEST_DATA" ) + exit 1 +fi + +exit 0 + diff --git a/tools/combine b/tools/combine index 3e2882d..ebd4dad 100755 --- a/tools/combine +++ b/tools/combine @@ -23,6 +23,7 @@ PATH_LIB="$PATH_TOOLS/../lib" MODULE_EXT='js' TPL_PATH="$PATH_TOOLS/combine.tpl" TPL_VAR='/**{CONTENT}**/' +RMTRAIL="$PATH_TOOLS/rmtrail" # order matters CAT_MODULES="util class interface" @@ -60,38 +61,6 @@ tpl_footer() }" } -## -# Removes trailing commas from array and object declarations (certain browsers, -# such as earlier versions of IE, do not parse trailing commas correctly) -# -# This is a very simple (dumb) system. It does not check to ensure we're not -# replacing text inside a string, nor is it 100% certain we're in an array or -# object declaration. However, until such an implementation is needed, I'd like -# to keep it as simple (and fast) as possible. The below implementation is -# suitable for our needs. -## -rmtrail() -{ - cat - \ - | sed -n ' - # copy first line to hold buffer - 1h - - # if not the first line, append to hold buffer - 1!H - - # if last line, process - $ { - # pull from hold and perform replacement - g - s/,\(\s*[]}]\)/\1/g - - # print result - p - } - ' -} - # ensure we can locate our template (should be in the /tools dir) if [ ! -f "$TPL_PATH" ]; then echo "Error: combine.tpl not found ($TPL_PATH)" @@ -117,7 +86,7 @@ for module in $CAT_MODULES; do echo "{" # add the module, removing trailing commas - cat $filename | rmtrail + cat $filename | $RMTRAIL echo "} )( exports['$module'] = {} );" done diff --git a/tools/rmtrail b/tools/rmtrail new file mode 100755 index 0000000..1e69603 --- /dev/null +++ b/tools/rmtrail @@ -0,0 +1,51 @@ +#!/bin/bash +# +# Removes trailing commas from array and object declarations (certain browsers, +# such as earlier versions of IE, do not parse trailing commas correctly) +# +# This is a very simple (dumb) system. It does not check to ensure we're not +# replacing text inside a string, nor is it 100% certain we're in an array or +# object declaration. However, until such an implementation is needed, I'd like +# to keep it as simple (and fast) as possible. The below implementation is +# suitable for our needs. +# +# Copyright (C) 2010 Mike Gerwitz +# +# This file is part of ease.js. +# +# ease.js is free software: you can redistribute it and/or modify it under the +# terms of the GNU Lesser General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License +# for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see . +# # + +# read from stdin +cat - \ + | sed -n ' + # copy first line to hold buffer + 1h + + # if not the first line, append to hold buffer + 1!H + + # if last line, process + $ { + # pull from hold and perform replacement + g + s/,\(\s*[]}]\)/\1/g + + # print result + p + } + ' + +exit 0 +