sakuric 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +13 -0
- data/Gemfile +22 -0
- data/Gemfile.lock +60 -0
- data/HISTORY.yml +13 -0
- data/INSTALL +5 -0
- data/LICENSE +1 -0
- data/Makefile +19 -0
- data/Manifest +405 -0
- data/README.md +72 -0
- data/Rakefile +4 -0
- data/TODO +3 -0
- data/VERSION +1 -0
- data/bashrc +30 -0
- data/bashrc.d/00-functions.include +7 -0
- data/bashrc.d/01-sakura_checks.include +15 -0
- data/bashrc.d/02-uname.include +11 -0
- data/bashrc.d/80-ruby.include +4 -0
- data/bashrc.d/99-dirs.include +11 -0
- data/bashrc.d/aliases.include +20 -0
- data/bashrc.d/common.include +7 -0
- data/bashrc.d/local.d/README +1 -0
- data/bashrc.d/local.d/nothing.include +0 -0
- data/bashrc.d/uname.d/Darwin +6 -0
- data/bashrc.d/uname.d/Linux +0 -0
- data/bashrc.local.sample +11 -0
- data/bin/10 +7 -0
- data/bin/10times +7 -0
- data/bin/1every +22 -0
- data/bin/1everyN +22 -0
- data/bin/1suN +22 -0
- data/bin/2 +5 -0
- data/bin/act +24 -0
- data/bin/amarelo +2 -0
- data/bin/arancio +4 -0
- data/bin/arcobaleno +10 -0
- data/bin/bianco +3 -0
- data/bin/black +2 -0
- data/bin/brew-install +3 -0
- data/bin/cache +50 -0
- data/bin/cache3 +50 -0
- data/bin/cheorae +4 -0
- data/bin/colorastdouterr +37 -0
- data/bin/cp2desktop +3 -0
- data/bin/create-git-repo.sh +24 -0
- data/bin/csoe +37 -0
- data/bin/data +3 -0
- data/bin/derive +90 -0
- data/bin/dimmi +31 -0
- data/bin/dimmiora +4 -0
- data/bin/doppioni +48 -0
- data/bin/du- +9 -0
- data/bin/duplicates +48 -0
- data/bin/echodo +7 -0
- data/bin/ensure_line_present +82 -0
- data/bin/epoch2date +5 -0
- data/bin/fanout +100 -0
- data/bin/find-broken-symlink +26 -0
- data/bin/find-duplicates +48 -0
- data/bin/find-special-chars-within-filenames +12 -0
- data/bin/fix-terminal +5 -0
- data/bin/gce-create-centos-instance +5 -0
- data/bin/gce-create-custom-image +8 -0
- data/bin/gce-curl-get-instances-for-project +7 -0
- data/bin/gce-get-token +3 -0
- data/bin/gce-getproject-metadata +35 -0
- data/bin/gce-public-ip +4 -0
- data/bin/gcutil-fetch +36 -0
- data/bin/giallo +2 -0
- data/bin/git-clone-in-non-empty-dir +21 -0
- data/bin/git-ignore +3 -0
- data/bin/git-repos +98 -0
- data/bin/git-repos.py +99 -0
- data/bin/gitinfo +2 -0
- data/bin/google-spreadsheet-cat +40 -0
- data/bin/google_ips +5 -0
- data/bin/gray +3 -0
- data/bin/green +2 -0
- data/bin/grigio +3 -0
- data/bin/gsutil-debug-bucket +12 -0
- data/bin/gsutil-get-oauth2-token +4 -0
- data/bin/gugol-image +74 -0
- data/bin/hamachi-ubuntu-install +17 -0
- data/bin/head-1^-1 +5 -0
- data/bin/ifdownup +13 -0
- data/bin/immature/ok2 +25 -0
- data/bin/immature/twice-still-broken +9 -0
- data/bin/install-gic-repo +13 -0
- data/bin/install-sakura-on-debian-latest +6 -0
- data/bin/iphoto_big_files.sh +37 -0
- data/bin/itunes +140 -0
- data/bin/jason-xpath.rb +54 -0
- data/bin/jsawk +1128 -0
- data/bin/json-xpath.rb +54 -0
- data/bin/keepup +13 -0
- data/bin/lsof-top10 +3 -0
- data/bin/minicook +218 -0
- data/bin/mv2. +2 -0
- data/bin/mv2desktop +2 -0
- data/bin/mv2dropbox +2 -0
- data/bin/mv2here +2 -0
- data/bin/mvto +39 -0
- data/bin/nero +2 -0
- data/bin/never_as_root +18 -0
- data/bin/not +16 -0
- data/bin/ok +42 -0
- data/bin/orange +4 -0
- data/bin/pink +2 -0
- data/bin/polygen-dell +7 -0
- data/bin/polygen-heanet +6 -0
- data/bin/polygen-labs +4 -0
- data/bin/print_key_val +4 -0
- data/bin/rainbow +10 -0
- data/bin/recipe +19 -0
- data/bin/red +2 -0
- data/bin/remember-command +140 -0
- data/bin/remember-command.rb +140 -0
- data/bin/richelp +99 -0
- data/bin/ricorda-comando +140 -0
- data/bin/rosa +2 -0
- data/bin/rosso +2 -0
- data/bin/run-parts.sh +50 -0
- data/bin/sakura-check-version +48 -0
- data/bin/sakura-init.DRAFT +5 -0
- data/bin/sakura-version +2 -0
- data/bin/sano +5 -0
- data/bin/sbianca +3 -0
- data/bin/sblua +2 -0
- data/bin/setterm-reset +5 -0
- data/bin/sgialla +2 -0
- data/bin/sgrigia +2 -0
- data/bin/split +37 -0
- data/bin/split.rb +37 -0
- data/bin/srosa +1 -0
- data/bin/srossa +2 -0
- data/bin/strip-white-spaces +2 -0
- data/bin/stty-sane +5 -0
- data/bin/sudo^-1 +18 -0
- data/bin/sverda +6 -0
- data/bin/swift-saio-install.sh +336 -0
- data/bin/synergyc +0 -0
- data/bin/synergys +0 -0
- data/bin/syracuse.pl +29 -0
- data/bin/tail-n+2 +5 -0
- data/bin/tellme +31 -0
- data/bin/tellme-time +4 -0
- data/bin/timeout3 +91 -0
- data/bin/top5 +4 -0
- data/bin/trim +4 -0
- data/bin/trova-broken-symlink +26 -0
- data/bin/twice +5 -0
- data/bin/twice.rb +5 -0
- data/bin/ubuntu-install-hamachi64 +17 -0
- data/bin/ultimo +5 -0
- data/bin/upload.py +2380 -0
- data/bin/usleep +0 -0
- data/bin/varia +90 -0
- data/bin/verde +2 -0
- data/bin/vermelho +2 -0
- data/bin/vim-whereveritis +5 -0
- data/bin/viola +1 -0
- data/bin/virsh-list-all-vms.py +21 -0
- data/bin/virtualbox-manage +41 -0
- data/bin/weekend +12 -0
- data/bin/whattimeisit +4 -0
- data/bin/whereis-ip +5 -0
- data/bin/white +3 -0
- data/bin/whitefy +3 -0
- data/bin/yellow +2 -0
- data/bin/yellowfy +2 -0
- data/bin/zombies +25 -0
- data/docz/polygen/dell.grm +100 -0
- data/docz/polygen/frati.grm +85 -0
- data/docz/polygen/heanet.grm +54 -0
- data/docz/richelp/gcompute.yml +52 -0
- data/docz/richelp/gcutil.yml +52 -0
- data/docz/richelp/gmail.yml +2 -0
- data/docz/richelp/ruby.yml +81 -0
- data/docz/richelp/sakura.yml +20 -0
- data/docz/richelp/sql.yml +12 -0
- data/docz/richelp/ubuntu.yml +22 -0
- data/docz/richelp/yaml.yml +39 -0
- data/docz/richelp/yml.yml +39 -0
- data/etc/sakura/devel.yml +36 -0
- data/etc/synergy.conf.example +37 -0
- data/images/color-sample.png +0 -0
- data/images/google.jpg +0 -0
- data/images/icons/toilet.png +0 -0
- data/images/sakura.jpg +0 -0
- data/images/sakura2.jpg +0 -0
- data/init.rb +5 -0
- data/lib/classes/arrays.rb +246 -0
- data/lib/classes/debug_ric.rb +196 -0
- data/lib/classes/fake_data.rb +8 -0
- data/lib/classes/ric_debug.rb +0 -0
- data/lib/classes/ricconf.rb +110 -0
- data/lib/classes/richelp.rb +70 -0
- data/lib/classes/strings.rb +284 -0
- data/lib/facter/google_compute_engine.rb +10 -0
- data/lib/recipes/20120411-puppet-rump-palladius.recipe +2 -0
- data/lib/recipes/20120726-skype.ubuntu.recipe +16 -0
- data/lib/recipes/20130206-puppet3.ubuntu.recipe +32 -0
- data/lib/recipes/20130206-redis.linux.recipe +17 -0
- data/lib/recipes/20130208-vanilla.linux.recipe +37 -0
- data/lib/recipes/Makefile +4 -0
- data/lib/recipes/README.md +29 -0
- data/lib/recipes/scripts/redis.sh +14 -0
- data/lib/ric.rb +156 -0
- data/lib/ric_colors.rb +313 -0
- data/lib/ric_strings.rb +285 -0
- data/lib/sonice-players/itunes_mac.rb +43 -0
- data/lib/sonice-players/itunes_win.rb +47 -0
- data/lib/sonice-players/mpd.rb +45 -0
- data/lib/sonice-players/rhythmbox.rb +38 -0
- data/profile +1 -0
- data/sakuric.gemspec +40 -0
- data/sbin/install-sakura-on-debian +25 -0
- data/sbin/make-install.sh +10 -0
- data/sbin/mate-createsymlink +4 -0
- data/sounds/0.ogg +0 -0
- data/sounds/0.wav +0 -0
- data/sounds/01.ogg +0 -0
- data/sounds/02.ogg +0 -0
- data/sounds/03.ogg +0 -0
- data/sounds/08.ogg +0 -0
- data/sounds/09.ogg +0 -0
- data/sounds/1.ogg +0 -0
- data/sounds/1.wav +0 -0
- data/sounds/10.ogg +0 -0
- data/sounds/100.ogg +0 -0
- data/sounds/11.ogg +0 -0
- data/sounds/12.ogg +0 -0
- data/sounds/13.ogg +0 -0
- data/sounds/14.ogg +0 -0
- data/sounds/15.ogg +0 -0
- data/sounds/16.ogg +0 -0
- data/sounds/17.ogg +0 -0
- data/sounds/18.ogg +0 -0
- data/sounds/19.ogg +0 -0
- data/sounds/2.ogg +0 -0
- data/sounds/2.wav +0 -0
- data/sounds/20.ogg +0 -0
- data/sounds/21.ogg +0 -0
- data/sounds/22.ogg +0 -0
- data/sounds/23.ogg +0 -0
- data/sounds/24.ogg +0 -0
- data/sounds/26.ogg +0 -0
- data/sounds/3.ogg +0 -0
- data/sounds/3.wav +0 -0
- data/sounds/30.ogg +0 -0
- data/sounds/34.ogg +0 -0
- data/sounds/4.ogg +0 -0
- data/sounds/4.wav +0 -0
- data/sounds/40.ogg +0 -0
- data/sounds/42.ogg +0 -0
- data/sounds/45.ogg +0 -0
- data/sounds/47.ogg +0 -0
- data/sounds/5.ogg +0 -0
- data/sounds/5.wav +0 -0
- data/sounds/50.ogg +0 -0
- data/sounds/6.ogg +0 -0
- data/sounds/6.wav +0 -0
- data/sounds/60.ogg +0 -0
- data/sounds/7.ogg +0 -0
- data/sounds/7.wav +0 -0
- data/sounds/70.ogg +0 -0
- data/sounds/8.ogg +0 -0
- data/sounds/8.wav +0 -0
- data/sounds/80.ogg +0 -0
- data/sounds/9.ogg +0 -0
- data/sounds/9.wav +0 -0
- data/sounds/90.ogg +0 -0
- data/sounds/a.ogg +0 -0
- data/sounds/aaahhh.ogg +0 -0
- data/sounds/abologna.ogg +0 -0
- data/sounds/adargenta.ogg +0 -0
- data/sounds/alzatiecammina.ogg +0 -0
- data/sounds/apache.ogg +0 -0
- data/sounds/attenzione.ogg +0 -0
- data/sounds/awesome.m4a +0 -0
- data/sounds/bazinga.mp3 +0 -0
- data/sounds/bazinga.ogg +0 -0
- data/sounds/bela regaz.wav +0 -0
- data/sounds/benvenuti.wav +0 -0
- data/sounds/benvenuti2.wav +0 -0
- data/sounds/bergonz.ogg +0 -0
- data/sounds/boh.ogg +0 -0
- data/sounds/buahah.ogg +0 -0
- data/sounds/buonasera.ogg +0 -0
- data/sounds/buongiorno.ogg +0 -0
- data/sounds/burp.ogg +0 -0
- data/sounds/carlo.ogg +0 -0
- data/sounds/cena.ogg +0 -0
- data/sounds/che figata.m4a +0 -0
- data/sounds/che figlio di puttana.m4a +0 -0
- data/sounds/che.ogg +0 -0
- data/sounds/ciao.ogg +0 -0
- data/sounds/ciao.wav +0 -0
- data/sounds/da.ogg +0 -0
- data/sounds/davide.ogg +0 -0
- data/sounds/demone.ogg +0 -0
- data/sounds/dhcp.ogg +0 -0
- data/sounds/dinuovo.ogg +0 -0
- data/sounds/dns.ogg +0 -0
- data/sounds/dopo.ogg +0 -0
- data/sounds/e'.ogg +0 -0
- data/sounds/eattivo.ogg +0 -0
- data/sounds/ee.ogg +0 -0
- data/sounds/ehi.ogg +0 -0
- data/sounds/eoragiu.ogg +0 -0
- data/sounds/eorasu.ogg +0 -0
- data/sounds/eripartita.ogg +0 -0
- data/sounds/estato.ogg +0 -0
- data/sounds/estatoriavviato.ogg +0 -0
- data/sounds/fratro.ogg +0 -0
- data/sounds/frozenbubble.ogg +0 -0
- data/sounds/funziona.ogg +0 -0
- data/sounds/go raibh mile maith agaibh.m4a +0 -0
- data/sounds/grazie.ogg +0 -0
- data/sounds/h1.ogg +0 -0
- data/sounds/hainuovaposta.ogg +0 -0
- data/sounds/hogiamangiato.ogg +0 -0
- data/sounds/host.ogg +0 -0
- data/sounds/il.ogg +0 -0
- data/sounds/ilcomputer.ogg +0 -0
- data/sounds/ilnumerodaleiselezionatoe.ogg +0 -0
- data/sounds/ilprocesso.ogg +0 -0
- data/sounds/ilprogramma.ogg +0 -0
- data/sounds/ilservizio.ogg +0 -0
- data/sounds/imap.ogg +0 -0
- data/sounds/imieisuperpoterimidiconoche.ogg +0 -0
- data/sounds/infunzione.ogg +0 -0
- data/sounds/inpunto.ogg +0 -0
- data/sounds/ipmon.ogg +0 -0
- data/sounds/laconnessioneadinternet.ogg +0 -0
- data/sounds/lastampante.ogg +0 -0
- data/sounds/lazzaron.ogg +0 -0
- data/sounds/ldap.ogg +0 -0
- data/sounds/linterfaccia.ogg +0 -0
- data/sounds/lucilla.ogg +0 -0
- data/sounds/ma vaffanculo.m4a +0 -0
- data/sounds/majjal.wav +0 -0
- data/sounds/mamma.ogg +0 -0
- data/sounds/mauro.ogg +0 -0
- data/sounds/max.ogg +0 -0
- data/sounds/meerda.wav +0 -0
- data/sounds/meno.ogg +0 -0
- data/sounds/merda clo.wav +0 -0
- data/sounds/mezza.ogg +0 -0
- data/sounds/mila.ogg +0 -0
- data/sounds/mille.ogg +0 -0
- data/sounds/minuti.ogg +0 -0
- data/sounds/named.ogg +0 -0
- data/sounds/no.ogg +0 -0
- data/sounds/nohofame.ogg +0 -0
- data/sounds/nonfunziona.ogg +0 -0
- data/sounds/nonriescoatrovare.ogg +0 -0
- data/sounds/nonsarebbeoradilavorare.ogg +0 -0
- data/sounds/nonstalavorando.ogg +0 -0
- data/sounds/numero.wav +0 -0
- data/sounds/ooohhh.ogg +0 -0
- data/sounds/pinger.ogg +0 -0
- data/sounds/pocofa.ogg +0 -0
- data/sounds/pop3.ogg +0 -0
- data/sounds/porca puttana.m4a +0 -0
- data/sounds/porcapupazza.ogg +0 -0
- data/sounds/ppp.ogg +0 -0
- data/sounds/pranzo.ogg +0 -0
- data/sounds/prego.ogg +0 -0
- data/sounds/prima.ogg +0 -0
- data/sounds/qualcosamidice.ogg +0 -0
- data/sounds/raggiungibile.ogg +0 -0
- data/sounds/riccardo.ogg +0 -0
- data/sounds/salsa ridge.wav +0 -0
- data/sounds/scusa.ogg +0 -0
- data/sounds/seriously man.m4a +0 -0
- data/sounds/si.ogg +0 -0
- data/sounds/sito porno con sonoro.wav +0 -0
- data/sounds/socmel.wav +0 -0
- data/sounds/sono.ogg +0 -0
- data/sounds/sonoacasa.ogg +0 -0
- data/sounds/sonole.ogg +0 -0
- data/sounds/sputo.ogg +0 -0
- data/sounds/ssh.ogg +0 -0
- data/sounds/stagiocando.ogg +0 -0
- data/sounds/statospento.ogg +0 -0
- data/sounds/stopaiodipalle.ogg +0 -0
- data/sounds/thesedicks.wav +0 -0
- data/sounds/tornatosu.ogg +0 -0
- data/sounds/trapoco.ogg +0 -0
- data/sounds/trequarti.ogg +0 -0
- data/sounds/tutti.ogg +0 -0
- data/sounds/una.ogg +0 -0
- data/sounds/unquarto.ogg +0 -0
- data/sounds/virgola.ogg +0 -0
- data/sources/c/usleep.c +50 -0
- data/spec/my_first_spec.rb +6 -0
- data/tasks/sakuric-gem.rake +21 -0
- data/tasks/testing.rake +15 -0
- data/templates/bashrc.inject +10 -0
- data/templates/happy_christmas.ascii_art +5 -0
- data/test/roodi.sh +10 -0
- data/test/test_mini_failing.rb +3 -0
- data/test/test_mini_ok.rb +3 -0
- data/test/test_ruby_syntax.sh +12 -0
- data/third-party/jsawk/README.markdown +437 -0
- data/third-party/jsawk/jsawk +1128 -0
- metadata +835 -0
data/bin/jsawk
ADDED
@@ -0,0 +1,1128 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
#
|
4
|
+
# Jsawk: It's like awk for JSON, in bash.
|
5
|
+
#
|
6
|
+
# Fork me on github:
|
7
|
+
# http://github.com/micha/jsawk
|
8
|
+
#
|
9
|
+
# Author:
|
10
|
+
# Micha Niskin <micha@thinkminimo.com>
|
11
|
+
# Copyright 2009, no rights reserved, other than as required by the
|
12
|
+
# licenses of the incorporated software below.
|
13
|
+
#
|
14
|
+
|
15
|
+
TMP1=`mktemp /tmp/tmp.XXXXXX`
|
16
|
+
TMP2=`mktemp /tmp/tmp.XXXXXX`
|
17
|
+
|
18
|
+
trap "rm -f $TMP1 $TMP2" SIGINT SIGTERM SIGHUP SIGQUIT
|
19
|
+
|
20
|
+
cat <<'__END__' > $TMP1
|
21
|
+
|
22
|
+
window = this; // the global object
|
23
|
+
window.IS = []; // the input set
|
24
|
+
window.RS = []; // the result set
|
25
|
+
window.$_ = {}; // the current element index
|
26
|
+
window.$$ = {}; // the current element
|
27
|
+
|
28
|
+
|
29
|
+
// Underscore.js 1.3.1
|
30
|
+
// (c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
|
31
|
+
// Underscore is freely distributable under the MIT license.
|
32
|
+
// Portions of Underscore are inspired or borrowed from Prototype,
|
33
|
+
// Oliver Steele's Functional, and John Resig's Micro-Templating.
|
34
|
+
// For all details and documentation:
|
35
|
+
// http://documentcloud.github.com/underscore
|
36
|
+
(function(){function q(a,c,d){if(a===c)return a!==0||1/a==1/c;if(a==null||c==null)return a===c;if(a._chain)a=a._wrapped;if(c._chain)c=c._wrapped;if(a.isEqual&&b.isFunction(a.isEqual))return a.isEqual(c);if(c.isEqual&&b.isFunction(c.isEqual))return c.isEqual(a);var e=l.call(a);if(e!=l.call(c))return false;switch(e){case "[object String]":return a==String(c);case "[object Number]":return a!=+a?c!=+c:a==0?1/a==1/c:a==+c;case "[object Date]":case "[object Boolean]":return+a==+c;case "[object RegExp]":return a.source==
|
37
|
+
c.source&&a.global==c.global&&a.multiline==c.multiline&&a.ignoreCase==c.ignoreCase}if(typeof a!="object"||typeof c!="object")return false;for(var f=d.length;f--;)if(d[f]==a)return true;d.push(a);var f=0,g=true;if(e=="[object Array]"){if(f=a.length,g=f==c.length)for(;f--;)if(!(g=f in a==f in c&&q(a[f],c[f],d)))break}else{if("constructor"in a!="constructor"in c||a.constructor!=c.constructor)return false;for(var h in a)if(b.has(a,h)&&(f++,!(g=b.has(c,h)&&q(a[h],c[h],d))))break;if(g){for(h in c)if(b.has(c,
|
38
|
+
h)&&!f--)break;g=!f}}d.pop();return g}var r=this,G=r._,n={},k=Array.prototype,o=Object.prototype,i=k.slice,H=k.unshift,l=o.toString,I=o.hasOwnProperty,w=k.forEach,x=k.map,y=k.reduce,z=k.reduceRight,A=k.filter,B=k.every,C=k.some,p=k.indexOf,D=k.lastIndexOf,o=Array.isArray,J=Object.keys,s=Function.prototype.bind,b=function(a){return new m(a)};if(typeof exports!=="undefined"){if(typeof module!=="undefined"&&module.exports)exports=module.exports=b;exports._=b}else r._=b;b.VERSION="1.3.1";var j=b.each=
|
39
|
+
b.forEach=function(a,c,d){if(a!=null)if(w&&a.forEach===w)a.forEach(c,d);else if(a.length===+a.length)for(var e=0,f=a.length;e<f;e++){if(e in a&&c.call(d,a[e],e,a)===n)break}else for(e in a)if(b.has(a,e)&&c.call(d,a[e],e,a)===n)break};b.map=b.collect=function(a,c,b){var e=[];if(a==null)return e;if(x&&a.map===x)return a.map(c,b);j(a,function(a,g,h){e[e.length]=c.call(b,a,g,h)});if(a.length===+a.length)e.length=a.length;return e};b.reduce=b.foldl=b.inject=function(a,c,d,e){var f=arguments.length>2;a==
|
40
|
+
null&&(a=[]);if(y&&a.reduce===y)return e&&(c=b.bind(c,e)),f?a.reduce(c,d):a.reduce(c);j(a,function(a,b,i){f?d=c.call(e,d,a,b,i):(d=a,f=true)});if(!f)throw new TypeError("Reduce of empty array with no initial value");return d};b.reduceRight=b.foldr=function(a,c,d,e){var f=arguments.length>2;a==null&&(a=[]);if(z&&a.reduceRight===z)return e&&(c=b.bind(c,e)),f?a.reduceRight(c,d):a.reduceRight(c);var g=b.toArray(a).reverse();e&&!f&&(c=b.bind(c,e));return f?b.reduce(g,c,d,e):b.reduce(g,c)};b.find=b.detect=
|
41
|
+
function(a,c,b){var e;E(a,function(a,g,h){if(c.call(b,a,g,h))return e=a,true});return e};b.filter=b.select=function(a,c,b){var e=[];if(a==null)return e;if(A&&a.filter===A)return a.filter(c,b);j(a,function(a,g,h){c.call(b,a,g,h)&&(e[e.length]=a)});return e};b.reject=function(a,c,b){var e=[];if(a==null)return e;j(a,function(a,g,h){c.call(b,a,g,h)||(e[e.length]=a)});return e};b.every=b.all=function(a,c,b){var e=true;if(a==null)return e;if(B&&a.every===B)return a.every(c,b);j(a,function(a,g,h){if(!(e=
|
42
|
+
e&&c.call(b,a,g,h)))return n});return e};var E=b.some=b.any=function(a,c,d){c||(c=b.identity);var e=false;if(a==null)return e;if(C&&a.some===C)return a.some(c,d);j(a,function(a,b,h){if(e||(e=c.call(d,a,b,h)))return n});return!!e};b.include=b.contains=function(a,c){var b=false;if(a==null)return b;return p&&a.indexOf===p?a.indexOf(c)!=-1:b=E(a,function(a){return a===c})};b.invoke=function(a,c){var d=i.call(arguments,2);return b.map(a,function(a){return(b.isFunction(c)?c||a:a[c]).apply(a,d)})};b.pluck=
|
43
|
+
function(a,c){return b.map(a,function(a){return a[c]})};b.max=function(a,c,d){if(!c&&b.isArray(a))return Math.max.apply(Math,a);if(!c&&b.isEmpty(a))return-Infinity;var e={computed:-Infinity};j(a,function(a,b,h){b=c?c.call(d,a,b,h):a;b>=e.computed&&(e={value:a,computed:b})});return e.value};b.min=function(a,c,d){if(!c&&b.isArray(a))return Math.min.apply(Math,a);if(!c&&b.isEmpty(a))return Infinity;var e={computed:Infinity};j(a,function(a,b,h){b=c?c.call(d,a,b,h):a;b<e.computed&&(e={value:a,computed:b})});
|
44
|
+
return e.value};b.shuffle=function(a){var b=[],d;j(a,function(a,f){f==0?b[0]=a:(d=Math.floor(Math.random()*(f+1)),b[f]=b[d],b[d]=a)});return b};b.sortBy=function(a,c,d){return b.pluck(b.map(a,function(a,b,g){return{value:a,criteria:c.call(d,a,b,g)}}).sort(function(a,b){var c=a.criteria,d=b.criteria;return c<d?-1:c>d?1:0}),"value")};b.groupBy=function(a,c){var d={},e=b.isFunction(c)?c:function(a){return a[c]};j(a,function(a,b){var c=e(a,b);(d[c]||(d[c]=[])).push(a)});return d};b.sortedIndex=function(a,
|
45
|
+
c,d){d||(d=b.identity);for(var e=0,f=a.length;e<f;){var g=e+f>>1;d(a[g])<d(c)?e=g+1:f=g}return e};b.toArray=function(a){return!a?[]:a.toArray?a.toArray():b.isArray(a)?i.call(a):b.isArguments(a)?i.call(a):b.values(a)};b.size=function(a){return b.toArray(a).length};b.first=b.head=function(a,b,d){return b!=null&&!d?i.call(a,0,b):a[0]};b.initial=function(a,b,d){return i.call(a,0,a.length-(b==null||d?1:b))};b.last=function(a,b,d){return b!=null&&!d?i.call(a,Math.max(a.length-b,0)):a[a.length-1]};b.rest=
|
46
|
+
b.tail=function(a,b,d){return i.call(a,b==null||d?1:b)};b.compact=function(a){return b.filter(a,function(a){return!!a})};b.flatten=function(a,c){return b.reduce(a,function(a,e){if(b.isArray(e))return a.concat(c?e:b.flatten(e));a[a.length]=e;return a},[])};b.without=function(a){return b.difference(a,i.call(arguments,1))};b.uniq=b.unique=function(a,c,d){var d=d?b.map(a,d):a,e=[];b.reduce(d,function(d,g,h){if(0==h||(c===true?b.last(d)!=g:!b.include(d,g)))d[d.length]=g,e[e.length]=a[h];return d},[]);
|
47
|
+
return e};b.union=function(){return b.uniq(b.flatten(arguments,true))};b.intersection=b.intersect=function(a){var c=i.call(arguments,1);return b.filter(b.uniq(a),function(a){return b.every(c,function(c){return b.indexOf(c,a)>=0})})};b.difference=function(a){var c=b.flatten(i.call(arguments,1));return b.filter(a,function(a){return!b.include(c,a)})};b.zip=function(){for(var a=i.call(arguments),c=b.max(b.pluck(a,"length")),d=Array(c),e=0;e<c;e++)d[e]=b.pluck(a,""+e);return d};b.indexOf=function(a,c,
|
48
|
+
d){if(a==null)return-1;var e;if(d)return d=b.sortedIndex(a,c),a[d]===c?d:-1;if(p&&a.indexOf===p)return a.indexOf(c);for(d=0,e=a.length;d<e;d++)if(d in a&&a[d]===c)return d;return-1};b.lastIndexOf=function(a,b){if(a==null)return-1;if(D&&a.lastIndexOf===D)return a.lastIndexOf(b);for(var d=a.length;d--;)if(d in a&&a[d]===b)return d;return-1};b.range=function(a,b,d){arguments.length<=1&&(b=a||0,a=0);for(var d=arguments[2]||1,e=Math.max(Math.ceil((b-a)/d),0),f=0,g=Array(e);f<e;)g[f++]=a,a+=d;return g};
|
49
|
+
var F=function(){};b.bind=function(a,c){var d,e;if(a.bind===s&&s)return s.apply(a,i.call(arguments,1));if(!b.isFunction(a))throw new TypeError;e=i.call(arguments,2);return d=function(){if(!(this instanceof d))return a.apply(c,e.concat(i.call(arguments)));F.prototype=a.prototype;var b=new F,g=a.apply(b,e.concat(i.call(arguments)));return Object(g)===g?g:b}};b.bindAll=function(a){var c=i.call(arguments,1);c.length==0&&(c=b.functions(a));j(c,function(c){a[c]=b.bind(a[c],a)});return a};b.memoize=function(a,
|
50
|
+
c){var d={};c||(c=b.identity);return function(){var e=c.apply(this,arguments);return b.has(d,e)?d[e]:d[e]=a.apply(this,arguments)}};b.delay=function(a,b){var d=i.call(arguments,2);return setTimeout(function(){return a.apply(a,d)},b)};b.defer=function(a){return b.delay.apply(b,[a,1].concat(i.call(arguments,1)))};b.throttle=function(a,c){var d,e,f,g,h,i=b.debounce(function(){h=g=false},c);return function(){d=this;e=arguments;var b;f||(f=setTimeout(function(){f=null;h&&a.apply(d,e);i()},c));g?h=true:
|
51
|
+
a.apply(d,e);i();g=true}};b.debounce=function(a,b){var d;return function(){var e=this,f=arguments;clearTimeout(d);d=setTimeout(function(){d=null;a.apply(e,f)},b)}};b.once=function(a){var b=false,d;return function(){if(b)return d;b=true;return d=a.apply(this,arguments)}};b.wrap=function(a,b){return function(){var d=[a].concat(i.call(arguments,0));return b.apply(this,d)}};b.compose=function(){var a=arguments;return function(){for(var b=arguments,d=a.length-1;d>=0;d--)b=[a[d].apply(this,b)];return b[0]}};
|
52
|
+
b.after=function(a,b){return a<=0?b():function(){if(--a<1)return b.apply(this,arguments)}};b.keys=J||function(a){if(a!==Object(a))throw new TypeError("Invalid object");var c=[],d;for(d in a)b.has(a,d)&&(c[c.length]=d);return c};b.values=function(a){return b.map(a,b.identity)};b.functions=b.methods=function(a){var c=[],d;for(d in a)b.isFunction(a[d])&&c.push(d);return c.sort()};b.extend=function(a){j(i.call(arguments,1),function(b){for(var d in b)a[d]=b[d]});return a};b.defaults=function(a){j(i.call(arguments,
|
53
|
+
1),function(b){for(var d in b)a[d]==null&&(a[d]=b[d])});return a};b.clone=function(a){return!b.isObject(a)?a:b.isArray(a)?a.slice():b.extend({},a)};b.tap=function(a,b){b(a);return a};b.isEqual=function(a,b){return q(a,b,[])};b.isEmpty=function(a){if(b.isArray(a)||b.isString(a))return a.length===0;for(var c in a)if(b.has(a,c))return false;return true};b.isElement=function(a){return!!(a&&a.nodeType==1)};b.isArray=o||function(a){return l.call(a)=="[object Array]"};b.isObject=function(a){return a===Object(a)};
|
54
|
+
b.isArguments=function(a){return l.call(a)=="[object Arguments]"};if(!b.isArguments(arguments))b.isArguments=function(a){return!(!a||!b.has(a,"callee"))};b.isFunction=function(a){return l.call(a)=="[object Function]"};b.isString=function(a){return l.call(a)=="[object String]"};b.isNumber=function(a){return l.call(a)=="[object Number]"};b.isNaN=function(a){return a!==a};b.isBoolean=function(a){return a===true||a===false||l.call(a)=="[object Boolean]"};b.isDate=function(a){return l.call(a)=="[object Date]"};
|
55
|
+
b.isRegExp=function(a){return l.call(a)=="[object RegExp]"};b.isNull=function(a){return a===null};b.isUndefined=function(a){return a===void 0};b.has=function(a,b){return I.call(a,b)};b.noConflict=function(){r._=G;return this};b.identity=function(a){return a};b.times=function(a,b,d){for(var e=0;e<a;e++)b.call(d,e)};b.escape=function(a){return(""+a).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'").replace(/\//g,"/")};b.mixin=function(a){j(b.functions(a),
|
56
|
+
function(c){K(c,b[c]=a[c])})};var L=0;b.uniqueId=function(a){var b=L++;return a?a+b:b};b.templateSettings={evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,escape:/<%-([\s\S]+?)%>/g};var t=/.^/,u=function(a){return a.replace(/\\\\/g,"\\").replace(/\\'/g,"'")};b.template=function(a,c){var d=b.templateSettings,d="var __p=[],print=function(){__p.push.apply(__p,arguments);};with(obj||{}){__p.push('"+a.replace(/\\/g,"\\\\").replace(/'/g,"\\'").replace(d.escape||t,function(a,b){return"',_.escape("+
|
57
|
+
u(b)+"),'"}).replace(d.interpolate||t,function(a,b){return"',"+u(b)+",'"}).replace(d.evaluate||t,function(a,b){return"');"+u(b).replace(/[\r\n\t]/g," ")+";__p.push('"}).replace(/\r/g,"\\r").replace(/\n/g,"\\n").replace(/\t/g,"\\t")+"');}return __p.join('');",e=new Function("obj","_",d);return c?e(c,b):function(a){return e.call(this,a,b)}};b.chain=function(a){return b(a).chain()};var m=function(a){this._wrapped=a};b.prototype=m.prototype;var v=function(a,c){return c?b(a).chain():a},K=function(a,c){m.prototype[a]=
|
58
|
+
function(){var a=i.call(arguments);H.call(a,this._wrapped);return v(c.apply(b,a),this._chain)}};b.mixin(b);j("pop,push,reverse,shift,sort,splice,unshift".split(","),function(a){var b=k[a];m.prototype[a]=function(){var d=this._wrapped;b.apply(d,arguments);var e=d.length;(a=="shift"||a=="splice")&&e===0&&delete d[0];return v(d,this._chain)}});j(["concat","join","slice"],function(a){var b=k[a];m.prototype[a]=function(){return v(b.apply(this._wrapped,arguments),this._chain)}});m.prototype.chain=function(){this._chain=
|
59
|
+
true;return this};m.prototype.value=function(){return this._wrapped}}).call(this);
|
60
|
+
|
61
|
+
(function() {
|
62
|
+
|
63
|
+
/*
|
64
|
+
Copyright Jason E. Smith 2008 Licensed under the Apache License, Version 2.0 (the "License");
|
65
|
+
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
66
|
+
*/
|
67
|
+
|
68
|
+
|
69
|
+
/*
|
70
|
+
* CREDITS:
|
71
|
+
* Thanks to Kris Zyp from SitePen for contributing his source for
|
72
|
+
* a standalone port of JSONQuery (from the dojox.json.query module).
|
73
|
+
*
|
74
|
+
* OVERVIEW:
|
75
|
+
* JSONQuery.js is a standalone port of the dojox.json.query module. It is intended as
|
76
|
+
* a dropin solution with zero dependencies. JSONQuery is intended to succeed and improve upon
|
77
|
+
* the JSONPath api (http://goessner.net/articles/JsonPath/) which offers rich powerful
|
78
|
+
* querying capabilities similar to those of XQuery.
|
79
|
+
*
|
80
|
+
* EXAMPLES / USAGE:
|
81
|
+
* see http://www.sitepen.com/blog/2008/07/16/jsonquery-data-querying-beyond-jsonpath/
|
82
|
+
*
|
83
|
+
* *Ripped from original source.
|
84
|
+
* JSONQuery(queryString,object)
|
85
|
+
and
|
86
|
+
JSONQuery(queryString)(object)
|
87
|
+
always return identical results. The first one immediately evaluates, the second one returns a
|
88
|
+
function that then evaluates the object.
|
89
|
+
|
90
|
+
example:
|
91
|
+
JSONQuery("foo",{foo:"bar"})
|
92
|
+
This will return "bar".
|
93
|
+
|
94
|
+
example:
|
95
|
+
evaluator = JSONQuery("?foo='bar'&rating>3");
|
96
|
+
This creates a function that finds all the objects in an array with a property
|
97
|
+
foo that is equals to "bar" and with a rating property with a value greater
|
98
|
+
than 3.
|
99
|
+
evaluator([{foo:"bar",rating:4},{foo:"baz",rating:2}])
|
100
|
+
This returns:
|
101
|
+
{foo:"bar",rating:4}
|
102
|
+
|
103
|
+
example:
|
104
|
+
evaluator = JSONQuery("$[?price<15.00][\rating][0:10]");
|
105
|
+
This finds objects in array with a price less than 15.00 and sorts then
|
106
|
+
by rating, highest rated first, and returns the first ten items in from this
|
107
|
+
filtered and sorted list.
|
108
|
+
|
109
|
+
|
110
|
+
example:
|
111
|
+
var data = {customers:[
|
112
|
+
{name:"Susan", purchases:29},
|
113
|
+
{name:"Kim", purchases:150},
|
114
|
+
{name:"Jake", purchases:27}
|
115
|
+
]};
|
116
|
+
|
117
|
+
var results = json.JSONQuery("$.customers[?purchases > 21 & name='Jake'][\\purchases]",data);
|
118
|
+
results
|
119
|
+
|
120
|
+
returns customers sorted by higest number of purchases to lowest.
|
121
|
+
|
122
|
+
*/
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
function map(arr, fun /*, thisp*/){
|
127
|
+
var len = arr.length;
|
128
|
+
if (typeof fun != "function")
|
129
|
+
throw new TypeError();
|
130
|
+
|
131
|
+
var res = new Array(len);
|
132
|
+
var thisp = arguments[2];
|
133
|
+
for (var i = 0; i < len; i++) {
|
134
|
+
if (i in arr)
|
135
|
+
res[i] = fun.call(thisp, arr[i], i, arr);
|
136
|
+
}
|
137
|
+
|
138
|
+
return res;
|
139
|
+
}
|
140
|
+
|
141
|
+
function filter(arr, fun /*, thisp*/){
|
142
|
+
var len = arr.length;
|
143
|
+
if (typeof fun != "function")
|
144
|
+
throw new TypeError();
|
145
|
+
|
146
|
+
var res = new Array();
|
147
|
+
var thisp = arguments[2];
|
148
|
+
for (var i = 0; i < len; i++) {
|
149
|
+
if (i in arr) {
|
150
|
+
var val = arr[i]; // in case fun mutates this
|
151
|
+
if (fun.call(thisp, val, i, arr))
|
152
|
+
res.push(val);
|
153
|
+
}
|
154
|
+
}
|
155
|
+
|
156
|
+
return res;
|
157
|
+
};
|
158
|
+
|
159
|
+
|
160
|
+
function slice(obj,start,end,step){
|
161
|
+
// handles slice operations: [3:6:2]
|
162
|
+
var len=obj.length,results = [];
|
163
|
+
end = end || len;
|
164
|
+
start = (start < 0) ? Math.max(0,start+len) : Math.min(len,start);
|
165
|
+
end = (end < 0) ? Math.max(0,end+len) : Math.min(len,end);
|
166
|
+
for(var i=start; i<end; i+=step){
|
167
|
+
results.push(obj[i]);
|
168
|
+
}
|
169
|
+
return results;
|
170
|
+
}
|
171
|
+
function expand(obj,name){
|
172
|
+
// handles ..name, .*, [*], [val1,val2], [val]
|
173
|
+
// name can be a property to search for, undefined for full recursive, or an array for picking by index
|
174
|
+
var results = [];
|
175
|
+
function walk(obj){
|
176
|
+
if(name){
|
177
|
+
if(name===true && !(obj instanceof Array)){
|
178
|
+
//recursive object search
|
179
|
+
results.push(obj);
|
180
|
+
}else if(obj[name]){
|
181
|
+
// found the name, add to our results
|
182
|
+
results.push(obj[name]);
|
183
|
+
}
|
184
|
+
}
|
185
|
+
for(var i in obj){
|
186
|
+
var val = obj[i];
|
187
|
+
if(!name){
|
188
|
+
// if we don't have a name we are just getting all the properties values (.* or [*])
|
189
|
+
results.push(val);
|
190
|
+
}else if(val && typeof val == 'object'){
|
191
|
+
|
192
|
+
walk(val);
|
193
|
+
}
|
194
|
+
}
|
195
|
+
}
|
196
|
+
if(name instanceof Array){
|
197
|
+
// this is called when multiple items are in the brackets: [3,4,5]
|
198
|
+
if(name.length==1){
|
199
|
+
// this can happen as a result of the parser becoming confused about commas
|
200
|
+
// in the brackets like [@.func(4,2)]. Fixing the parser would require recursive
|
201
|
+
// analsys, very expensive, but this fixes the problem nicely.
|
202
|
+
return obj[name[0]];
|
203
|
+
}
|
204
|
+
for(var i = 0; i < name.length; i++){
|
205
|
+
results.push(obj[name[i]]);
|
206
|
+
}
|
207
|
+
}else{
|
208
|
+
// otherwise we expanding
|
209
|
+
walk(obj);
|
210
|
+
}
|
211
|
+
return results;
|
212
|
+
}
|
213
|
+
|
214
|
+
function distinctFilter(array, callback){
|
215
|
+
// does the filter with removal of duplicates in O(n)
|
216
|
+
var outArr = [];
|
217
|
+
var primitives = {};
|
218
|
+
for(var i=0,l=array.length; i<l; ++i){
|
219
|
+
var value = array[i];
|
220
|
+
if(callback(value, i, array)){
|
221
|
+
if(!primitives[JSON.stringify(value)]){
|
222
|
+
// with primitives we prevent duplicates by putting it in a map
|
223
|
+
primitives[JSON.stringify(value)] = true;
|
224
|
+
outArr.push(value);
|
225
|
+
}
|
226
|
+
}
|
227
|
+
}
|
228
|
+
for(i=0,l=outArr.length; i<l; ++i){
|
229
|
+
// cleanup the marker properties
|
230
|
+
if(outArr[i]){
|
231
|
+
delete outArr[i].__included;
|
232
|
+
}
|
233
|
+
}
|
234
|
+
return outArr;
|
235
|
+
}
|
236
|
+
window.uniq = function(array) {
|
237
|
+
return distinctFilter(array, function() { return true; });
|
238
|
+
};
|
239
|
+
var JSONQuery = function(/*String*/query,/*Object?*/obj){
|
240
|
+
// summary:
|
241
|
+
// Performs a JSONQuery on the provided object and returns the results.
|
242
|
+
// If no object is provided (just a query), it returns a "compiled" function that evaluates objects
|
243
|
+
// according to the provided query.
|
244
|
+
// query:
|
245
|
+
// Query string
|
246
|
+
// obj:
|
247
|
+
// Target of the JSONQuery
|
248
|
+
//
|
249
|
+
// description:
|
250
|
+
// JSONQuery provides a comprehensive set of data querying tools including filtering,
|
251
|
+
// recursive search, sorting, mapping, range selection, and powerful expressions with
|
252
|
+
// wildcard string comparisons and various operators. JSONQuery generally supersets
|
253
|
+
// JSONPath and provides syntax that matches and behaves like JavaScript where
|
254
|
+
// possible.
|
255
|
+
//
|
256
|
+
// JSONQuery evaluations begin with the provided object, which can referenced with
|
257
|
+
// $. From
|
258
|
+
// the starting object, various operators can be successively applied, each operating
|
259
|
+
// on the result of the last operation.
|
260
|
+
//
|
261
|
+
// Supported Operators:
|
262
|
+
// --------------------
|
263
|
+
// * .property - This will return the provided property of the object, behaving exactly
|
264
|
+
// like JavaScript.
|
265
|
+
// * [expression] - This returns the property name/index defined by the evaluation of
|
266
|
+
// the provided expression, behaving exactly like JavaScript.
|
267
|
+
// * [?expression] - This will perform a filter operation on an array, returning all the
|
268
|
+
// items in an array that match the provided expression. This operator does not
|
269
|
+
// need to be in brackets, you can simply use ?expression, but since it does not
|
270
|
+
// have any containment, no operators can be used afterwards when used
|
271
|
+
// without brackets.
|
272
|
+
// * [^?expression] - This will perform a distinct filter operation on an array. This behaves
|
273
|
+
// as [?expression] except that it will remove any duplicate values/objects from the
|
274
|
+
// result set.
|
275
|
+
// * [/expression], [\expression], [/expression, /expression] - This performs a sort
|
276
|
+
// operation on an array, with sort based on the provide expression. Multiple comma delimited sort
|
277
|
+
// expressions can be provided for multiple sort orders (first being highest priority). /
|
278
|
+
// indicates ascending order and \ indicates descending order
|
279
|
+
// * [=expression] - This performs a map operation on an array, creating a new array
|
280
|
+
// with each item being the evaluation of the expression for each item in the source array.
|
281
|
+
// * [start:end:step] - This performs an array slice/range operation, returning the elements
|
282
|
+
// from the optional start index to the optional end index, stepping by the optional step number.
|
283
|
+
// * [expr,expr] - This a union operator, returning an array of all the property/index values from
|
284
|
+
// the evaluation of the comma delimited expressions.
|
285
|
+
// * .* or [*] - This returns the values of all the properties of the current object.
|
286
|
+
// * $ - This is the root object, If a JSONQuery expression does not being with a $,
|
287
|
+
// it will be auto-inserted at the beginning.
|
288
|
+
// * @ - This is the current object in filter, sort, and map expressions. This is generally
|
289
|
+
// not necessary, names are auto-converted to property references of the current object
|
290
|
+
// in expressions.
|
291
|
+
// * ..property - Performs a recursive search for the given property name, returning
|
292
|
+
// an array of all values with such a property name in the current object and any subobjects
|
293
|
+
// * expr = expr - Performs a comparison (like JS's ==). When comparing to
|
294
|
+
// a string, the comparison string may contain wildcards * (matches any number of
|
295
|
+
// characters) and ? (matches any single character).
|
296
|
+
// * expr ~ expr - Performs a string comparison with case insensitivity.
|
297
|
+
// * ..[?expression] - This will perform a deep search filter operation on all the objects and
|
298
|
+
// subobjects of the current data. Rather than only searching an array, this will search
|
299
|
+
// property values, arrays, and their children.
|
300
|
+
// * $1,$2,$3, etc. - These are references to extra parameters passed to the query
|
301
|
+
// function or the evaluator function.
|
302
|
+
// * +, -, /, *, &, |, %, (, ), <, >, <=, >=, != - These operators behave just as they do
|
303
|
+
// in JavaScript.
|
304
|
+
//
|
305
|
+
//
|
306
|
+
//
|
307
|
+
// | dojox.json.query(queryString,object)
|
308
|
+
// and
|
309
|
+
// | dojox.json.query(queryString)(object)
|
310
|
+
// always return identical results. The first one immediately evaluates, the second one returns a
|
311
|
+
// function that then evaluates the object.
|
312
|
+
//
|
313
|
+
// example:
|
314
|
+
// | dojox.json.query("foo",{foo:"bar"})
|
315
|
+
// This will return "bar".
|
316
|
+
//
|
317
|
+
// example:
|
318
|
+
// | evaluator = dojox.json.query("?foo='bar'&rating>3");
|
319
|
+
// This creates a function that finds all the objects in an array with a property
|
320
|
+
// foo that is equals to "bar" and with a rating property with a value greater
|
321
|
+
// than 3.
|
322
|
+
// | evaluator([{foo:"bar",rating:4},{foo:"baz",rating:2}])
|
323
|
+
// This returns:
|
324
|
+
// | {foo:"bar",rating:4}
|
325
|
+
//
|
326
|
+
// example:
|
327
|
+
// | evaluator = dojox.json.query("$[?price<15.00][\rating][0:10]");
|
328
|
+
// This finds objects in array with a price less than 15.00 and sorts then
|
329
|
+
// by rating, highest rated first, and returns the first ten items in from this
|
330
|
+
// filtered and sorted list.
|
331
|
+
tokens = [];
|
332
|
+
var depth = 0;
|
333
|
+
var str = [];
|
334
|
+
query = query.replace(/"(\\.|[^"\\])*"|'(\\.|[^'\\])*'|[\[\]]/g,function(t){
|
335
|
+
depth += t == '[' ? 1 : t == ']' ? -1 : 0; // keep track of bracket depth
|
336
|
+
return (t == ']' && depth > 0) ? '`]' : // we mark all the inner brackets as skippable
|
337
|
+
(t.charAt(0) == '"' || t.charAt(0) == "'") ? "`" + (str.push(t) - 1) :// and replace all the strings
|
338
|
+
t;
|
339
|
+
});
|
340
|
+
var prefix = '';
|
341
|
+
function call(name){
|
342
|
+
// creates a function call and puts the expression so far in a parameter for a call
|
343
|
+
prefix = name + "(" + prefix;
|
344
|
+
}
|
345
|
+
function makeRegex(t,a,b,c,d){
|
346
|
+
// creates a regular expression matcher for when wildcards and ignore case is used
|
347
|
+
return str[d].match(/[\*\?]/) || c == '~' ?
|
348
|
+
"/^" + str[d].substring(1,str[d].length-1).replace(/\\([btnfr\\"'])|([^\w\*\?])/g,"\\$1$2").replace(/([\*\?])/g,".$1") + (c == '~' ? '$/i' : '$/') + ".test(" + a + ")" :
|
349
|
+
t;
|
350
|
+
}
|
351
|
+
query.replace(/(\]|\)|push|pop|shift|splice|sort|reverse)\s*\(/,function(){
|
352
|
+
throw new Error("Unsafe function call");
|
353
|
+
});
|
354
|
+
|
355
|
+
query = query.replace(/([^=]=)([^=])/g,"$1=$2"). // change the equals to comparisons
|
356
|
+
replace(/@|(\.\s*)?[a-zA-Z\$_]+(\s*:)?/g,function(t){
|
357
|
+
return t.charAt(0) == '.' ? t : // leave .prop alone
|
358
|
+
t == '@' ? "$obj" :// the reference to the current object
|
359
|
+
(t.match(/:|^(\$|Math|true|false|null)$/) ? "" : "$obj.") + t; // plain names should be properties of root... unless they are a label in object initializer
|
360
|
+
}).
|
361
|
+
replace(/\.?\.?\[(`\]|[^\]])*\]|\?.*|\.\.([\w\$_]+)|\.\*/g,function(t,a,b){
|
362
|
+
var oper = t.match(/^\.?\.?(\[\s*\^?\?|\^?\?|\[\s*==)(.*?)\]?$/); // [?expr] and ?expr and [=expr and =expr
|
363
|
+
if(oper){
|
364
|
+
var prefix = '';
|
365
|
+
if(t.match(/^\./)){
|
366
|
+
// recursive object search
|
367
|
+
call("expand");
|
368
|
+
prefix = ",true)";
|
369
|
+
}
|
370
|
+
call(oper[1].match(/\=/) ? "map" : oper[1].match(/\^/) ? "distinctFilter" : "filter");
|
371
|
+
return prefix + ",function($obj){return " + oper[2] + "})";
|
372
|
+
}
|
373
|
+
oper = t.match(/^\[\s*([\/\\].*)\]/); // [/sortexpr,\sortexpr]
|
374
|
+
if(oper){
|
375
|
+
// make a copy of the array and then sort it using the sorting expression
|
376
|
+
return ".concat().sort(function(a,b){" + oper[1].replace(/\s*,?\s*([\/\\])\s*([^,\\\/]+)/g,function(t,a,b){
|
377
|
+
return "var av= " + b.replace(/\$obj/,"a") + ",bv= " + b.replace(/\$obj/,"b") + // FIXME: Should check to make sure the $obj token isn't followed by characters
|
378
|
+
";if(av>bv||bv==null){return " + (a== "/" ? 1 : -1) +";}\n" +
|
379
|
+
"if(bv>av||av==null){return " + (a== "/" ? -1 : 1) +";}\n";
|
380
|
+
}) + "})";
|
381
|
+
}
|
382
|
+
oper = t.match(/^\[(-?[0-9]*):(-?[0-9]*):?(-?[0-9]*)\]/); // slice [0:3]
|
383
|
+
if(oper){
|
384
|
+
call("slice");
|
385
|
+
return "," + (oper[1] || 0) + "," + (oper[2] || 0) + "," + (oper[3] || 1) + ")";
|
386
|
+
}
|
387
|
+
if(t.match(/^\.\.|\.\*|\[\s*\*\s*\]|,/)){ // ..prop and [*]
|
388
|
+
call("expand");
|
389
|
+
return (t.charAt(1) == '.' ?
|
390
|
+
",'" + b + "'" : // ..prop
|
391
|
+
t.match(/,/) ?
|
392
|
+
"," + t : // [prop1,prop2]
|
393
|
+
"") + ")"; // [*]
|
394
|
+
}
|
395
|
+
return t;
|
396
|
+
}).
|
397
|
+
replace(/(\$obj\s*(\.\s*[\w_$]+\s*)*)(==|~)\s*`([0-9]+)/g,makeRegex). // create regex matching
|
398
|
+
replace(/`([0-9]+)\s*(==|~)\s*(\$obj(\s*\.\s*[\w_$]+)*)/g,function(t,a,b,c,d){ // and do it for reverse =
|
399
|
+
return makeRegex(t,c,d,b,a);
|
400
|
+
});
|
401
|
+
query = prefix + (query.charAt(0) == '$' ? "" : "$") + query.replace(/`([0-9]+|\])/g,function(t,a){
|
402
|
+
//restore the strings
|
403
|
+
return a == ']' ? ']' : str[a];
|
404
|
+
});
|
405
|
+
// create a function within this scope (so it can use expand and slice)
|
406
|
+
|
407
|
+
var executor = eval("1&&function($,$1,$2,$3,$4,$5,$6,$7,$8,$9){var $obj=$;return " + query + "}");
|
408
|
+
for(var i = 0;i<arguments.length-1;i++){
|
409
|
+
arguments[i] = arguments[i+1];
|
410
|
+
}
|
411
|
+
return obj ? executor.apply(this,arguments) : executor;
|
412
|
+
};
|
413
|
+
|
414
|
+
|
415
|
+
if(typeof namespace == "function"){
|
416
|
+
namespace("json::JSONQuery", JSONQuery);
|
417
|
+
}
|
418
|
+
else {
|
419
|
+
window["JSONQuery"] = JSONQuery;
|
420
|
+
}
|
421
|
+
})();
|
422
|
+
|
423
|
+
/*
|
424
|
+
http://www.JSON.org/json2.js
|
425
|
+
2009-04-16
|
426
|
+
|
427
|
+
Public Domain.
|
428
|
+
|
429
|
+
NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
|
430
|
+
|
431
|
+
See http://www.JSON.org/js.html
|
432
|
+
|
433
|
+
This file creates a global JSON object containing two methods: stringify
|
434
|
+
and parse.
|
435
|
+
|
436
|
+
JSON.stringify(value, replacer, space)
|
437
|
+
value any JavaScript value, usually an object or array.
|
438
|
+
|
439
|
+
replacer an optional parameter that determines how object
|
440
|
+
values are stringified for objects. It can be a
|
441
|
+
function or an array of strings.
|
442
|
+
|
443
|
+
space an optional parameter that specifies the indentation
|
444
|
+
of nested structures. If it is omitted, the text will
|
445
|
+
be packed without extra whitespace. If it is a number,
|
446
|
+
it will specify the number of spaces to indent at each
|
447
|
+
level. If it is a string (such as '\t' or ' '),
|
448
|
+
it contains the characters used to indent at each level.
|
449
|
+
|
450
|
+
This method produces a JSON text from a JavaScript value.
|
451
|
+
|
452
|
+
When an object value is found, if the object contains a toJSON
|
453
|
+
method, its toJSON method will be called and the result will be
|
454
|
+
stringified. A toJSON method does not serialize: it returns the
|
455
|
+
value represented by the name/value pair that should be serialized,
|
456
|
+
or undefined if nothing should be serialized. The toJSON method
|
457
|
+
will be passed the key associated with the value, and this will be
|
458
|
+
bound to the object holding the key.
|
459
|
+
|
460
|
+
For example, this would serialize Dates as ISO strings.
|
461
|
+
|
462
|
+
Date.prototype.toJSON = function (key) {
|
463
|
+
function f(n) {
|
464
|
+
// Format integers to have at least two digits.
|
465
|
+
return n < 10 ? '0' + n : n;
|
466
|
+
}
|
467
|
+
|
468
|
+
return this.getUTCFullYear() + '-' +
|
469
|
+
f(this.getUTCMonth() + 1) + '-' +
|
470
|
+
f(this.getUTCDate()) + 'T' +
|
471
|
+
f(this.getUTCHours()) + ':' +
|
472
|
+
f(this.getUTCMinutes()) + ':' +
|
473
|
+
f(this.getUTCSeconds()) + 'Z';
|
474
|
+
};
|
475
|
+
|
476
|
+
You can provide an optional replacer method. It will be passed the
|
477
|
+
key and value of each member, with this bound to the containing
|
478
|
+
object. The value that is returned from your method will be
|
479
|
+
serialized. If your method returns undefined, then the member will
|
480
|
+
be excluded from the serialization.
|
481
|
+
|
482
|
+
If the replacer parameter is an array of strings, then it will be
|
483
|
+
used to select the members to be serialized. It filters the results
|
484
|
+
such that only members with keys listed in the replacer array are
|
485
|
+
stringified.
|
486
|
+
|
487
|
+
Values that do not have JSON representations, such as undefined or
|
488
|
+
functions, will not be serialized. Such values in objects will be
|
489
|
+
dropped; in arrays they will be replaced with null. You can use
|
490
|
+
a replacer function to replace those with JSON values.
|
491
|
+
JSON.stringify(undefined) returns undefined.
|
492
|
+
|
493
|
+
The optional space parameter produces a stringification of the
|
494
|
+
value that is filled with line breaks and indentation to make it
|
495
|
+
easier to read.
|
496
|
+
|
497
|
+
If the space parameter is a non-empty string, then that string will
|
498
|
+
be used for indentation. If the space parameter is a number, then
|
499
|
+
the indentation will be that many spaces.
|
500
|
+
|
501
|
+
Example:
|
502
|
+
|
503
|
+
text = JSON.stringify(['e', {pluribus: 'unum'}]);
|
504
|
+
// text is '["e",{"pluribus":"unum"}]'
|
505
|
+
|
506
|
+
|
507
|
+
text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t');
|
508
|
+
// text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
|
509
|
+
|
510
|
+
text = JSON.stringify([new Date()], function (key, value) {
|
511
|
+
return this[key] instanceof Date ?
|
512
|
+
'Date(' + this[key] + ')' : value;
|
513
|
+
});
|
514
|
+
// text is '["Date(---current time---)"]'
|
515
|
+
|
516
|
+
|
517
|
+
JSON.parse(text, reviver)
|
518
|
+
This method parses a JSON text to produce an object or array.
|
519
|
+
It can throw a SyntaxError exception.
|
520
|
+
|
521
|
+
The optional reviver parameter is a function that can filter and
|
522
|
+
transform the results. It receives each of the keys and values,
|
523
|
+
and its return value is used instead of the original value.
|
524
|
+
If it returns what it received, then the structure is not modified.
|
525
|
+
If it returns undefined then the member is deleted.
|
526
|
+
|
527
|
+
Example:
|
528
|
+
|
529
|
+
// Parse the text. Values that look like ISO date strings will
|
530
|
+
// be converted to Date objects.
|
531
|
+
|
532
|
+
myData = JSON.parse(text, function (key, value) {
|
533
|
+
var a;
|
534
|
+
if (typeof value === 'string') {
|
535
|
+
a =
|
536
|
+
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
|
537
|
+
if (a) {
|
538
|
+
return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
|
539
|
+
+a[5], +a[6]));
|
540
|
+
}
|
541
|
+
}
|
542
|
+
return value;
|
543
|
+
});
|
544
|
+
|
545
|
+
myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) {
|
546
|
+
var d;
|
547
|
+
if (typeof value === 'string' &&
|
548
|
+
value.slice(0, 5) === 'Date(' &&
|
549
|
+
value.slice(-1) === ')') {
|
550
|
+
d = new Date(value.slice(5, -1));
|
551
|
+
if (d) {
|
552
|
+
return d;
|
553
|
+
}
|
554
|
+
}
|
555
|
+
return value;
|
556
|
+
});
|
557
|
+
|
558
|
+
|
559
|
+
This is a reference implementation. You are free to copy, modify, or
|
560
|
+
redistribute.
|
561
|
+
|
562
|
+
This code should be minified before deployment.
|
563
|
+
See http://javascript.crockford.com/jsmin.html
|
564
|
+
|
565
|
+
USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
|
566
|
+
NOT CONTROL.
|
567
|
+
*/
|
568
|
+
|
569
|
+
/*jslint evil: true */
|
570
|
+
|
571
|
+
/*global JSON */
|
572
|
+
|
573
|
+
/*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply,
|
574
|
+
call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
|
575
|
+
getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
|
576
|
+
lastIndex, length, parse, prototype, push, replace, slice, stringify,
|
577
|
+
test, toJSON, toString, valueOf
|
578
|
+
*/
|
579
|
+
|
580
|
+
// Create a JSON object only if one does not already exist. We create the
|
581
|
+
// methods in a closure to avoid creating global variables.
|
582
|
+
|
583
|
+
if (!this.JSON) {
|
584
|
+
JSON = {};
|
585
|
+
}
|
586
|
+
(function () {
|
587
|
+
|
588
|
+
function f(n) {
|
589
|
+
// Format integers to have at least two digits.
|
590
|
+
return n < 10 ? '0' + n : n;
|
591
|
+
}
|
592
|
+
|
593
|
+
if (typeof Date.prototype.toJSON !== 'function') {
|
594
|
+
|
595
|
+
Date.prototype.toJSON = function (key) {
|
596
|
+
|
597
|
+
return this.getUTCFullYear() + '-' +
|
598
|
+
f(this.getUTCMonth() + 1) + '-' +
|
599
|
+
f(this.getUTCDate()) + 'T' +
|
600
|
+
f(this.getUTCHours()) + ':' +
|
601
|
+
f(this.getUTCMinutes()) + ':' +
|
602
|
+
f(this.getUTCSeconds()) + 'Z';
|
603
|
+
};
|
604
|
+
|
605
|
+
String.prototype.toJSON =
|
606
|
+
Number.prototype.toJSON =
|
607
|
+
Boolean.prototype.toJSON = function (key) {
|
608
|
+
return this.valueOf();
|
609
|
+
};
|
610
|
+
}
|
611
|
+
|
612
|
+
var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
|
613
|
+
escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
|
614
|
+
gap,
|
615
|
+
indent,
|
616
|
+
meta = { // table of character substitutions
|
617
|
+
'\b': '\\b',
|
618
|
+
'\t': '\\t',
|
619
|
+
'\n': '\\n',
|
620
|
+
'\f': '\\f',
|
621
|
+
'\r': '\\r',
|
622
|
+
'"' : '\\"',
|
623
|
+
'\\': '\\\\'
|
624
|
+
},
|
625
|
+
rep;
|
626
|
+
|
627
|
+
|
628
|
+
function quote(string) {
|
629
|
+
|
630
|
+
// If the string contains no control characters, no quote characters, and no
|
631
|
+
// backslash characters, then we can safely slap some quotes around it.
|
632
|
+
// Otherwise we must also replace the offending characters with safe escape
|
633
|
+
// sequences.
|
634
|
+
|
635
|
+
escapable.lastIndex = 0;
|
636
|
+
return escapable.test(string) ?
|
637
|
+
'"' + string.replace(escapable, function (a) {
|
638
|
+
var c = meta[a];
|
639
|
+
return typeof c === 'string' ? c :
|
640
|
+
'\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
|
641
|
+
}) + '"' :
|
642
|
+
'"' + string + '"';
|
643
|
+
}
|
644
|
+
|
645
|
+
|
646
|
+
function str(key, holder) {
|
647
|
+
|
648
|
+
// Produce a string from holder[key].
|
649
|
+
|
650
|
+
var i, // The loop counter.
|
651
|
+
k, // The member key.
|
652
|
+
v, // The member value.
|
653
|
+
length,
|
654
|
+
mind = gap,
|
655
|
+
partial,
|
656
|
+
value = holder[key];
|
657
|
+
|
658
|
+
// If the value has a toJSON method, call it to obtain a replacement value.
|
659
|
+
|
660
|
+
if (value && typeof value === 'object' &&
|
661
|
+
typeof value.toJSON === 'function') {
|
662
|
+
value = value.toJSON(key);
|
663
|
+
}
|
664
|
+
|
665
|
+
// If we were called with a replacer function, then call the replacer to
|
666
|
+
// obtain a replacement value.
|
667
|
+
|
668
|
+
if (typeof rep === 'function') {
|
669
|
+
value = rep.call(holder, key, value);
|
670
|
+
}
|
671
|
+
|
672
|
+
// What happens next depends on the value's type.
|
673
|
+
|
674
|
+
switch (typeof value) {
|
675
|
+
case 'string':
|
676
|
+
return quote(value);
|
677
|
+
|
678
|
+
case 'number':
|
679
|
+
|
680
|
+
// JSON numbers must be finite. Encode non-finite numbers as null.
|
681
|
+
|
682
|
+
return isFinite(value) ? String(value) : 'null';
|
683
|
+
|
684
|
+
case 'boolean':
|
685
|
+
case 'null':
|
686
|
+
|
687
|
+
// If the value is a boolean or null, convert it to a string. Note:
|
688
|
+
// typeof null does not produce 'null'. The case is included here in
|
689
|
+
// the remote chance that this gets fixed someday.
|
690
|
+
|
691
|
+
return String(value);
|
692
|
+
|
693
|
+
// If the type is 'object', we might be dealing with an object or an array or
|
694
|
+
// null.
|
695
|
+
|
696
|
+
case 'object':
|
697
|
+
|
698
|
+
// Due to a specification blunder in ECMAScript, typeof null is 'object',
|
699
|
+
// so watch out for that case.
|
700
|
+
|
701
|
+
if (!value) {
|
702
|
+
return 'null';
|
703
|
+
}
|
704
|
+
|
705
|
+
// Make an array to hold the partial results of stringifying this object value.
|
706
|
+
|
707
|
+
gap += indent;
|
708
|
+
partial = [];
|
709
|
+
|
710
|
+
// Is the value an array?
|
711
|
+
|
712
|
+
if (Object.prototype.toString.apply(value) === '[object Array]') {
|
713
|
+
|
714
|
+
// The value is an array. Stringify every element. Use null as a placeholder
|
715
|
+
// for non-JSON values.
|
716
|
+
|
717
|
+
length = value.length;
|
718
|
+
for (i = 0; i < length; i += 1) {
|
719
|
+
partial[i] = str(i, value) || 'null';
|
720
|
+
}
|
721
|
+
|
722
|
+
// Join all of the elements together, separated with commas, and wrap them in
|
723
|
+
// brackets.
|
724
|
+
|
725
|
+
v = partial.length === 0 ? '[]' :
|
726
|
+
gap ? '[\n' + gap +
|
727
|
+
partial.join(',\n' + gap) + '\n' +
|
728
|
+
mind + ']' :
|
729
|
+
'[' + partial.join(',') + ']';
|
730
|
+
gap = mind;
|
731
|
+
return v;
|
732
|
+
}
|
733
|
+
|
734
|
+
// If the replacer is an array, use it to select the members to be stringified.
|
735
|
+
|
736
|
+
if (rep && typeof rep === 'object') {
|
737
|
+
length = rep.length;
|
738
|
+
for (i = 0; i < length; i += 1) {
|
739
|
+
k = rep[i];
|
740
|
+
if (typeof k === 'string') {
|
741
|
+
v = str(k, value);
|
742
|
+
if (v) {
|
743
|
+
partial.push(quote(k) + (gap ? ': ' : ':') + v);
|
744
|
+
}
|
745
|
+
}
|
746
|
+
}
|
747
|
+
} else {
|
748
|
+
|
749
|
+
// Otherwise, iterate through all of the keys in the object.
|
750
|
+
|
751
|
+
for (k in value) {
|
752
|
+
if (Object.hasOwnProperty.call(value, k)) {
|
753
|
+
v = str(k, value);
|
754
|
+
if (v) {
|
755
|
+
partial.push(quote(k) + (gap ? ': ' : ':') + v);
|
756
|
+
}
|
757
|
+
}
|
758
|
+
}
|
759
|
+
}
|
760
|
+
|
761
|
+
// Join all of the member texts together, separated with commas,
|
762
|
+
// and wrap them in braces.
|
763
|
+
|
764
|
+
v = partial.length === 0 ? '{}' :
|
765
|
+
gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' +
|
766
|
+
mind + '}' : '{' + partial.join(',') + '}';
|
767
|
+
gap = mind;
|
768
|
+
return v;
|
769
|
+
}
|
770
|
+
}
|
771
|
+
|
772
|
+
// If the JSON object does not yet have a stringify method, give it one.
|
773
|
+
|
774
|
+
if (typeof JSON.stringify !== 'function') {
|
775
|
+
JSON.stringify = function (value, replacer, space) {
|
776
|
+
|
777
|
+
// The stringify method takes a value and an optional replacer, and an optional
|
778
|
+
// space parameter, and returns a JSON text. The replacer can be a function
|
779
|
+
// that can replace values, or an array of strings that will select the keys.
|
780
|
+
// A default replacer method can be provided. Use of the space parameter can
|
781
|
+
// produce text that is more easily readable.
|
782
|
+
|
783
|
+
var i;
|
784
|
+
gap = '';
|
785
|
+
indent = '';
|
786
|
+
|
787
|
+
// If the space parameter is a number, make an indent string containing that
|
788
|
+
// many spaces.
|
789
|
+
|
790
|
+
if (typeof space === 'number') {
|
791
|
+
for (i = 0; i < space; i += 1) {
|
792
|
+
indent += ' ';
|
793
|
+
}
|
794
|
+
|
795
|
+
// If the space parameter is a string, it will be used as the indent string.
|
796
|
+
|
797
|
+
} else if (typeof space === 'string') {
|
798
|
+
indent = space;
|
799
|
+
}
|
800
|
+
|
801
|
+
// If there is a replacer, it must be a function or an array.
|
802
|
+
// Otherwise, throw an error.
|
803
|
+
|
804
|
+
rep = replacer;
|
805
|
+
if (replacer && typeof replacer !== 'function' &&
|
806
|
+
(typeof replacer !== 'object' ||
|
807
|
+
typeof replacer.length !== 'number')) {
|
808
|
+
throw new Error('JSON.stringify');
|
809
|
+
}
|
810
|
+
|
811
|
+
// Make a fake root object containing our value under the key of ''.
|
812
|
+
// Return the result of stringifying the value.
|
813
|
+
|
814
|
+
return str('', {'': value});
|
815
|
+
};
|
816
|
+
}
|
817
|
+
|
818
|
+
|
819
|
+
// If the JSON object does not yet have a parse method, give it one.
|
820
|
+
|
821
|
+
if (typeof JSON.parse !== 'function') {
|
822
|
+
JSON.parse = function (text, reviver) {
|
823
|
+
|
824
|
+
// The parse method takes a text and an optional reviver function, and returns
|
825
|
+
// a JavaScript value if the text is a valid JSON text.
|
826
|
+
|
827
|
+
var j;
|
828
|
+
|
829
|
+
function walk(holder, key) {
|
830
|
+
|
831
|
+
// The walk method is used to recursively walk the resulting structure so
|
832
|
+
// that modifications can be made.
|
833
|
+
|
834
|
+
var k, v, value = holder[key];
|
835
|
+
if (value && typeof value === 'object') {
|
836
|
+
for (k in value) {
|
837
|
+
if (Object.hasOwnProperty.call(value, k)) {
|
838
|
+
v = walk(value, k);
|
839
|
+
if (v !== undefined) {
|
840
|
+
value[k] = v;
|
841
|
+
} else {
|
842
|
+
delete value[k];
|
843
|
+
}
|
844
|
+
}
|
845
|
+
}
|
846
|
+
}
|
847
|
+
return reviver.call(holder, key, value);
|
848
|
+
}
|
849
|
+
|
850
|
+
|
851
|
+
// Parsing happens in four stages. In the first stage, we replace certain
|
852
|
+
// Unicode characters with escape sequences. JavaScript handles many characters
|
853
|
+
// incorrectly, either silently deleting them, or treating them as line endings.
|
854
|
+
|
855
|
+
cx.lastIndex = 0;
|
856
|
+
if (cx.test(text)) {
|
857
|
+
text = text.replace(cx, function (a) {
|
858
|
+
return '\\u' +
|
859
|
+
('0000' + a.charCodeAt(0).toString(16)).slice(-4);
|
860
|
+
});
|
861
|
+
}
|
862
|
+
|
863
|
+
// In the second stage, we run the text against regular expressions that look
|
864
|
+
// for non-JSON patterns. We are especially concerned with '()' and 'new'
|
865
|
+
// because they can cause invocation, and '=' because it can cause mutation.
|
866
|
+
// But just to be safe, we want to reject all unexpected forms.
|
867
|
+
|
868
|
+
// We split the second stage into 4 regexp operations in order to work around
|
869
|
+
// crippling inefficiencies in IE's and Safari's regexp engines. First we
|
870
|
+
// replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
|
871
|
+
// replace all simple value tokens with ']' characters. Third, we delete all
|
872
|
+
// open brackets that follow a colon or comma or that begin the text. Finally,
|
873
|
+
// we look to see that the remaining characters are only whitespace or ']' or
|
874
|
+
// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
|
875
|
+
|
876
|
+
if (/^[\],:{}\s]*$/.
|
877
|
+
test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').
|
878
|
+
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
|
879
|
+
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
|
880
|
+
|
881
|
+
// In the third stage we use the eval function to compile the text into a
|
882
|
+
// JavaScript structure. The '{' operator is subject to a syntactic ambiguity
|
883
|
+
// in JavaScript: it can begin a block or an object literal. We wrap the text
|
884
|
+
// in parens to eliminate the ambiguity.
|
885
|
+
|
886
|
+
j = eval('(' + text + ')');
|
887
|
+
|
888
|
+
// In the optional fourth stage, we recursively walk the new structure, passing
|
889
|
+
// each name/value pair to a reviver function for possible transformation.
|
890
|
+
|
891
|
+
return typeof reviver === 'function' ?
|
892
|
+
walk({'': j}, '') : j;
|
893
|
+
}
|
894
|
+
|
895
|
+
// If the text is not JSON parseable, then a SyntaxError is thrown.
|
896
|
+
|
897
|
+
throw new SyntaxError('JSON.parse');
|
898
|
+
};
|
899
|
+
}
|
900
|
+
}());
|
901
|
+
|
902
|
+
(function(p) {
|
903
|
+
var doPrint = function() {
|
904
|
+
var args = Array.prototype.slice.call(arguments);
|
905
|
+
var type = args.shift();
|
906
|
+
var input = args.join(" ");
|
907
|
+
var lines = input.split("\n");
|
908
|
+
for (var i in lines)
|
909
|
+
p(type, lines[i]);
|
910
|
+
};
|
911
|
+
var doJsonPrint = function() {
|
912
|
+
var args = Array.prototype.slice.call(arguments);
|
913
|
+
var type = args.shift();
|
914
|
+
if (args.length > 0) {
|
915
|
+
args = args.length > 1 ? args : args[0];
|
916
|
+
var ret = typeof(args) == "string" ? args : json(args);
|
917
|
+
doPrint(type, ret);
|
918
|
+
}
|
919
|
+
};
|
920
|
+
window.Q = function() {
|
921
|
+
try {
|
922
|
+
var ret = JSONQuery.apply(window, arguments);
|
923
|
+
ret.length;
|
924
|
+
return ret;
|
925
|
+
} catch (e) {
|
926
|
+
err("jsawk: JSONQuery parse error: '"+arguments[0]+"'");
|
927
|
+
quit(4);
|
928
|
+
}
|
929
|
+
};
|
930
|
+
window.out = function() {
|
931
|
+
var args = Array.prototype.slice.call(arguments);
|
932
|
+
args.unshift("OUT:");
|
933
|
+
doJsonPrint.apply(window, args);
|
934
|
+
};
|
935
|
+
window.err = function() {
|
936
|
+
var args = Array.prototype.slice.call(arguments);
|
937
|
+
args.unshift("ERR:");
|
938
|
+
doJsonPrint.apply(window, args);
|
939
|
+
};
|
940
|
+
window.alert = p;
|
941
|
+
window.doJson = function(input) {
|
942
|
+
if (typeof input !== "string") {
|
943
|
+
return input;
|
944
|
+
} else {
|
945
|
+
input = input.replace(/\s*$/,"");
|
946
|
+
if (!input.length) {
|
947
|
+
return {};
|
948
|
+
} else {
|
949
|
+
try {
|
950
|
+
return eval("("+input+")");
|
951
|
+
} catch (e) {
|
952
|
+
err("jsawk: JSON parse error: '"+input+"'");
|
953
|
+
quit(2);
|
954
|
+
}
|
955
|
+
}
|
956
|
+
}
|
957
|
+
};
|
958
|
+
window.doCall = function(fun, obj) {
|
959
|
+
try {
|
960
|
+
return fun.call(obj);
|
961
|
+
} catch (e) {
|
962
|
+
err("jsawk: js error: "+e);
|
963
|
+
quit(3);
|
964
|
+
}
|
965
|
+
};
|
966
|
+
window.makeFilter = function(fun) {
|
967
|
+
try {
|
968
|
+
return eval("(function() { "+fun+"; return this })");
|
969
|
+
} catch (e) {
|
970
|
+
err("jsawk: script parse error: '"+fun+"'");
|
971
|
+
quit(3);
|
972
|
+
}
|
973
|
+
};
|
974
|
+
window.json = function() {
|
975
|
+
try {
|
976
|
+
return JSON.stringify.apply(window, arguments);
|
977
|
+
} catch (e) {
|
978
|
+
err("jsawk: JSON stringify error: "+e);
|
979
|
+
quit(5);
|
980
|
+
}
|
981
|
+
};
|
982
|
+
window.get = function() {
|
983
|
+
return $$ = IS[++$_];
|
984
|
+
};
|
985
|
+
window.put = function(record) {
|
986
|
+
IS = IS.slice(0, $_+1).concat([record]).concat(IS.slice($_+1));
|
987
|
+
};
|
988
|
+
window.forEach = function(ary, fun) {
|
989
|
+
fun = eval("function(index,item) { "+fun+"; }");
|
990
|
+
for (var i=0; i<ary.length; i++) {
|
991
|
+
try {
|
992
|
+
fun.call(ary[i], i, ary[i]);
|
993
|
+
} catch (e) {
|
994
|
+
err("jsawk: js error: "+e);
|
995
|
+
quit(3);
|
996
|
+
}
|
997
|
+
}
|
998
|
+
};
|
999
|
+
})(window.print);
|
1000
|
+
|
1001
|
+
(function(argv) {
|
1002
|
+
argv = Array.prototype.slice.call(argv);
|
1003
|
+
|
1004
|
+
var inputLines = argv.shift();
|
1005
|
+
|
1006
|
+
var usage = function() {
|
1007
|
+
err("Usage: jsq [-n] [-f jsfile1.js]* [-q jsonquery] "+
|
1008
|
+
"[-b script] \\\n"+
|
1009
|
+
" [-a script] [-v NAME=VALUE] [script]");
|
1010
|
+
quit(1);
|
1011
|
+
};
|
1012
|
+
|
1013
|
+
var fun = "";
|
1014
|
+
var noprint = false;
|
1015
|
+
var libs = [];
|
1016
|
+
var befores = [];
|
1017
|
+
var afters = [];
|
1018
|
+
var queries = [];
|
1019
|
+
var i,j,k,l,m,n;
|
1020
|
+
|
1021
|
+
var arg;
|
1022
|
+
|
1023
|
+
while (arg = argv.shift()) {
|
1024
|
+
switch(arg) {
|
1025
|
+
case "-h":
|
1026
|
+
usage();
|
1027
|
+
break;
|
1028
|
+
case "-n":
|
1029
|
+
noprint = true;
|
1030
|
+
break;
|
1031
|
+
case "-q":
|
1032
|
+
if (argv.length < 1) usage();
|
1033
|
+
queries.push(argv.shift());
|
1034
|
+
break;
|
1035
|
+
case "-f":
|
1036
|
+
if (argv.length < 1) usage();
|
1037
|
+
libs.push(argv.shift());
|
1038
|
+
break;
|
1039
|
+
case "-b":
|
1040
|
+
if (argv.length < 1) usage();
|
1041
|
+
befores.push(makeFilter(argv.shift()));
|
1042
|
+
break;
|
1043
|
+
case "-a":
|
1044
|
+
if (argv.length < 1) usage();
|
1045
|
+
afters.push(makeFilter(argv.shift()));
|
1046
|
+
break;
|
1047
|
+
case "-v":
|
1048
|
+
if (argv.length < 1) usage();
|
1049
|
+
var tmp = argv.shift();
|
1050
|
+
var key = tmp.replace(/=.*$/, "");
|
1051
|
+
var val = tmp.replace(/^[^=]+=/, "");
|
1052
|
+
window[key] = val;
|
1053
|
+
break;
|
1054
|
+
default:
|
1055
|
+
fun = arg;
|
1056
|
+
}
|
1057
|
+
}
|
1058
|
+
|
1059
|
+
var input="";
|
1060
|
+
|
1061
|
+
for (var i=0; i<inputLines; i++) {
|
1062
|
+
var line=readline();
|
1063
|
+
input += (line ? line : "");
|
1064
|
+
input += "\n";
|
1065
|
+
}
|
1066
|
+
|
1067
|
+
var wrapped;
|
1068
|
+
|
1069
|
+
IS = doJson(input);
|
1070
|
+
wrapped = !(IS instanceof Array);
|
1071
|
+
|
1072
|
+
if (wrapped)
|
1073
|
+
IS = [ IS ];
|
1074
|
+
|
1075
|
+
for (i in libs)
|
1076
|
+
load(libs[i]);
|
1077
|
+
|
1078
|
+
var f = makeFilter(fun);
|
1079
|
+
|
1080
|
+
for (i in queries)
|
1081
|
+
IS = Q(queries[i], IS);
|
1082
|
+
|
1083
|
+
$_ = -1;
|
1084
|
+
$$ = IS;
|
1085
|
+
|
1086
|
+
for (i in befores)
|
1087
|
+
IS = doCall(befores[i], IS);
|
1088
|
+
|
1089
|
+
RS = [];
|
1090
|
+
|
1091
|
+
for ($_=0; $_<IS.length; $_++) {
|
1092
|
+
$$ = IS[$_]
|
1093
|
+
var tmp = doCall(f, $$);
|
1094
|
+
if (tmp != null) RS.push(tmp);
|
1095
|
+
}
|
1096
|
+
|
1097
|
+
$_ = -1;
|
1098
|
+
$$ = RS;
|
1099
|
+
|
1100
|
+
for (i in afters)
|
1101
|
+
RS = doCall(afters[i], RS);
|
1102
|
+
|
1103
|
+
if (wrapped)
|
1104
|
+
RS = RS.pop();
|
1105
|
+
|
1106
|
+
if (!noprint)
|
1107
|
+
out(RS);
|
1108
|
+
})(arguments);
|
1109
|
+
__END__
|
1110
|
+
|
1111
|
+
nlines=0
|
1112
|
+
|
1113
|
+
if [ "$1" != "-h" ]; then
|
1114
|
+
echo "$(cat 2>/dev/null)" > $TMP2
|
1115
|
+
nlines=$(grep -c '$' $TMP2 2>/dev/null || echo 0)
|
1116
|
+
fi
|
1117
|
+
|
1118
|
+
res=$(cat $TMP2 2>/dev/null | js $TMP1 $nlines "$@")
|
1119
|
+
ret=$?
|
1120
|
+
out=$(echo "$res" |sed '/^OUT: /s/^.....//p;d')
|
1121
|
+
err=$(echo "$res" |sed '/^ERR: /s/^.....//p;d')
|
1122
|
+
|
1123
|
+
[ -n "$err" ] && echo "$err" 1>&2
|
1124
|
+
[ -n "$out" ] && echo "$out"
|
1125
|
+
|
1126
|
+
rm -f $TMP1 $TMP2
|
1127
|
+
|
1128
|
+
exit $ret
|