Skip to content

Commit

Permalink
svgtiler.match and svgtiler.filter (#99)
Browse files Browse the repository at this point in the history
More glob helpers via minimatch
  • Loading branch information
edemaine committed Oct 14, 2022
1 parent f7cae35 commit bab785b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions examples/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ all:
svgtiler -f context.coffee context.asc
svgtiler -f escape.txt escape.csv
svgtiler -f css-escape.css css-escape.txt css-escape.asc
svgtiler -f glob.coffee
svgtiler -f mapping.coffee mapping.ssv
svgtiler -f mappings.coffee -O mappings mapping.ssv
svgtiler -f set.coffee set.asc
Expand Down
3 changes: 2 additions & 1 deletion examples/test/Maketile.args
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
( context.coffee context.asc )

( escape.txt escape.csv )

( css-escape.css css-escape.txt css-escape.asc )

( glob.coffee )

( mapping.coffee mapping.ssv )
( mappings.coffee -O mappings mapping.ssv )

Expand Down
14 changes: 14 additions & 0 deletions examples/test/glob.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
same = (array1, array2) ->
return false unless array1.length == array2.length
for item, index in array2
return false unless item == array2[index]
true

assertSame = (array1, array2, help) ->
console.assert same(array1, array2), help

assertSame svgtiler.glob('glob.*'), ['glob.coffee'], 'glob'
assertSame svgtiler.match('glob.coffee', 'glob.*'), true, 'match 1'
assertSame svgtiler.match('glob.js', 'glob.*'), true, 'match 2'
assertSame svgtiler.match('foo.coffee', 'glob.*'), false, 'match 3'
assertSame svgtiler.filter(['glob.coffee', 'glob.js', 'foo.coffee'], 'glob.*'), ['glob.coffee', 'glob.js'], 'filter'
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"glob": "8.0.3",
"grapheme-splitter": "1.0.4",
"image-size": "1.0.1",
"minimatch": "5.1.0",
"pirates": "4.0.5",
"preact": "10.7.2",
"preact-html-converter": "0.4.2",
Expand Down
9 changes: 8 additions & 1 deletion src/svgtiler.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2366,6 +2366,13 @@ glob = (pattern, options) ->
pattern = pattern.replace /\\($|[^\*\+\?\!\|\@\(\)\[\]\{\}])/g, '/$1'
require('glob').sync pattern, options

match = (filename, pattern, options) ->
## 'glob' library uses 'minimatch' to match filenames against glob patterns.
Boolean require('minimatch').match [filename], pattern, options

filter = (filenames, pattern, options) ->
require('minimatch').match filenames, pattern, options

loadMaketile = (settings = getSettings() ? defaultSettings) ->
filenames = glob settings.maketile, nodir: true
return unless filenames.length
Expand Down Expand Up @@ -2561,7 +2568,7 @@ svgtiler = {
add: globalAdd,
Context, getContext, getContextString, runWithContext,
SVGTilerError, SVGNS, XLINKNS, escapeId,
main, renderDOM, convert, glob, require: inputRequire,
main, renderDOM, convert, glob, match, filter, require: inputRequire,
defaultSettings, getSettings, cloneSettings, getSetting, getOutputDir,
share: globalShare
version: metadata.version
Expand Down

0 comments on commit bab785b

Please sign in to comment.