opal 0.9.4 → 0.10.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitattributes +1 -0
- data/.gitignore +2 -3
- data/.gitmodules +5 -2
- data/.jshintrc +1 -8
- data/.rspec +1 -1
- data/.travis.yml +15 -23
- data/CHANGELOG.md +511 -326
- data/CODE_OF_CONDUCT.md +13 -15
- data/CONTRIBUTING.md +26 -216
- data/Gemfile +20 -12
- data/Guardfile +2 -2
- data/HACKING.md +230 -0
- data/README.md +6 -7
- data/bin/opal-mspec +1 -1
- data/config.ru +2 -2
- data/docs/faq.md +1 -1
- data/docs/source_maps.md +1 -1
- data/lib/opal.rb +1 -0
- data/lib/opal/builder.rb +1 -1
- data/lib/opal/cli.rb +30 -28
- data/lib/opal/cli_options.rb +3 -0
- data/lib/opal/cli_runners.rb +14 -1
- data/lib/opal/cli_runners/{apple_script.rb → applescript.rb} +3 -3
- data/lib/opal/cli_runners/nashorn.rb +2 -2
- data/lib/opal/cli_runners/nodejs.rb +2 -2
- data/lib/opal/cli_runners/phantom.js +24 -0
- data/lib/opal/cli_runners/phantomjs.rb +10 -10
- data/lib/opal/cli_runners/server.rb +3 -3
- data/lib/opal/compiler.rb +43 -4
- data/lib/opal/config.rb +3 -1
- data/lib/opal/errors.rb +13 -0
- data/lib/opal/fragment.rb +0 -13
- data/lib/opal/nodes.rb +10 -0
- data/lib/opal/nodes/args/initialize_kwargs.rb +28 -0
- data/lib/opal/nodes/args/kwarg.rb +29 -0
- data/lib/opal/nodes/args/kwoptarg.rb +29 -0
- data/lib/opal/nodes/args/kwrestarg.rb +39 -0
- data/lib/opal/nodes/args/mlhsarg.rb +79 -0
- data/lib/opal/nodes/args/normarg.rb +26 -0
- data/lib/opal/nodes/args/optarg.rb +27 -0
- data/lib/opal/nodes/args/post_args.rb +200 -0
- data/lib/opal/nodes/args/post_kwargs.rb +31 -0
- data/lib/opal/nodes/args/restarg.rb +33 -0
- data/lib/opal/nodes/base.rb +12 -0
- data/lib/opal/nodes/call.rb +92 -33
- data/lib/opal/nodes/def.rb +26 -169
- data/lib/opal/nodes/hash.rb +10 -4
- data/lib/opal/nodes/helpers.rb +6 -3
- data/lib/opal/nodes/inline_args.rb +61 -0
- data/lib/opal/nodes/iter.rb +73 -82
- data/lib/opal/nodes/logic.rb +12 -2
- data/lib/opal/nodes/masgn.rb +1 -2
- data/lib/opal/nodes/node_with_args.rb +141 -0
- data/lib/opal/nodes/rescue.rb +121 -43
- data/lib/opal/nodes/scope.rb +24 -5
- data/lib/opal/nodes/super.rb +122 -54
- data/lib/opal/nodes/top.rb +0 -12
- data/lib/opal/nodes/yield.rb +2 -13
- data/lib/opal/parser.rb +67 -39
- data/lib/opal/parser/grammar.rb +3319 -2961
- data/lib/opal/parser/grammar.y +234 -46
- data/lib/opal/parser/lexer.rb +105 -17
- data/lib/opal/parser/sexp.rb +4 -0
- data/lib/opal/paths.rb +4 -0
- data/lib/opal/regexp_anchors.rb +19 -1
- data/lib/opal/sprockets.rb +21 -18
- data/lib/opal/sprockets/environment.rb +0 -8
- data/lib/opal/sprockets/processor.rb +13 -16
- data/lib/opal/sprockets/server.rb +6 -12
- data/lib/opal/version.rb +1 -1
- data/opal.gemspec +1 -0
- data/opal/corelib/array.rb +209 -131
- data/opal/corelib/basic_object.rb +7 -3
- data/opal/corelib/class.rb +11 -17
- data/opal/corelib/constants.rb +2 -2
- data/opal/corelib/enumerable.rb +178 -355
- data/opal/corelib/enumerator.rb +3 -46
- data/opal/corelib/error.rb +2 -2
- data/opal/corelib/file.rb +13 -1
- data/opal/corelib/hash.rb +26 -56
- data/opal/corelib/helpers.rb +10 -0
- data/opal/corelib/kernel.rb +6 -3
- data/opal/corelib/module.rb +62 -31
- data/opal/corelib/number.rb +7 -16
- data/opal/corelib/proc.rb +24 -9
- data/opal/corelib/range.rb +4 -13
- data/opal/corelib/runtime.js +515 -378
- data/opal/corelib/string.rb +21 -49
- data/opal/corelib/struct.rb +50 -35
- data/opal/corelib/unsupported.rb +18 -30
- data/opal/opal.rb +0 -1
- data/opal/opal/mini.rb +1 -0
- data/spec/README.md +6 -4
- data/spec/filters/bugs/array.rb +0 -42
- data/spec/filters/bugs/basicobject.rb +0 -2
- data/spec/filters/bugs/bigdecimal.rb +160 -0
- data/spec/filters/bugs/class.rb +0 -5
- data/spec/filters/bugs/date.rb +1 -48
- data/spec/filters/bugs/enumerable.rb +4 -12
- data/spec/filters/bugs/enumerator.rb +0 -1
- data/spec/filters/bugs/exception.rb +4 -3
- data/spec/filters/bugs/float.rb +4 -2
- data/spec/filters/bugs/kernel.rb +25 -10
- data/spec/filters/bugs/language.rb +119 -68
- data/spec/filters/bugs/method.rb +135 -0
- data/spec/filters/bugs/module.rb +13 -28
- data/spec/filters/bugs/proc.rb +18 -8
- data/spec/filters/bugs/range.rb +0 -3
- data/spec/filters/bugs/rational.rb +4 -0
- data/spec/filters/bugs/regexp.rb +68 -36
- data/spec/filters/bugs/string.rb +1 -1
- data/spec/filters/bugs/struct.rb +0 -12
- data/spec/filters/bugs/time.rb +1 -0
- data/spec/filters/bugs/unboundmethod.rb +2 -1
- data/spec/filters/unsupported/freeze.rb +3 -1
- data/spec/filters/unsupported/language.rb +0 -7
- data/spec/filters/unsupported/privacy.rb +7 -6
- data/spec/filters/unsupported/string.rb +10 -0
- data/spec/filters/unsupported/struct.rb +3 -0
- data/spec/filters/unsupported/symbol.rb +9 -0
- data/spec/filters/unsupported/taint.rb +0 -3
- data/spec/filters/unsupported/thread.rb +1 -0
- data/spec/lib/cli_runners/phantomjs_spec.rb +39 -0
- data/spec/lib/cli_spec.rb +42 -1
- data/spec/lib/compiler/call_spec.rb +700 -0
- data/spec/lib/compiler_spec.rb +46 -28
- data/spec/lib/config_spec.rb +13 -0
- data/spec/lib/parser/call_spec.rb +18 -0
- data/spec/lib/parser/def_spec.rb +29 -0
- data/spec/lib/parser/iter_spec.rb +15 -15
- data/spec/lib/parser/lambda_spec.rb +153 -12
- data/spec/lib/parser/string_spec.rb +5 -0
- data/spec/lib/parser/undef_spec.rb +1 -1
- data/spec/lib/parser/variables_spec.rb +24 -0
- data/spec/lib/paths_spec.rb +12 -5
- data/spec/lib/spec_helper.rb +5 -0
- data/spec/lib/sprockets/processor_spec.rb +6 -5
- data/spec/lib/sprockets_spec.rb +8 -0
- data/spec/mspec-opal/formatters.rb +188 -0
- data/spec/mspec-opal/runner.rb +193 -0
- data/spec/opal/core/enumerator/with_index_spec.rb +6 -0
- data/spec/opal/core/kernel/define_singleton_method_spec.rb +1 -1
- data/spec/opal/core/kernel/instance_variables_spec.rb +14 -0
- data/spec/opal/core/kernel/loop_spec.rb +1 -1
- data/spec/opal/core/kernel/raise_spec.rb +1 -1
- data/spec/opal/core/language/heredoc_spec.rb +42 -0
- data/spec/opal/core/language/rescue_spec.rb +18 -0
- data/spec/opal/core/language_spec.rb +22 -0
- data/spec/opal/core/module/const_defined_spec.rb +1 -2
- data/spec/opal/core/module/name_spec.rb +6 -0
- data/spec/opal/core/runtime/bridged_classes_spec.rb +14 -2
- data/spec/opal/core/runtime/rescue_spec.rb +12 -2
- data/spec/opal/core/runtime/super_spec.rb +1 -0
- data/spec/opal/core/string_spec.rb +21 -0
- data/spec/opal/stdlib/js_spec.rb +1 -1
- data/spec/opal/stdlib/native/hash_spec.rb +7 -0
- data/spec/opal/stdlib/promise/always_spec.rb +24 -5
- data/spec/opal/stdlib/promise/rescue_spec.rb +15 -6
- data/spec/opal/stdlib/promise/then_spec.rb +13 -5
- data/spec/opal/stdlib/promise/trace_spec.rb +5 -6
- data/spec/opal/stdlib/strscan/scan_spec.rb +1 -1
- data/spec/ruby_specs +122 -0
- data/spec/spec_helper.rb +3 -15
- data/stdlib/base64.rb +51 -121
- data/stdlib/bigdecimal.rb +231 -0
- data/stdlib/bigdecimal/bignumber.js.rb +11 -0
- data/stdlib/bigdecimal/kernel.rb +5 -0
- data/stdlib/date.rb +252 -10
- data/stdlib/native.rb +38 -38
- data/stdlib/nodejs/dir.rb +8 -6
- data/stdlib/nodejs/file.rb +28 -3
- data/stdlib/nodejs/node_modules/.bin/js-yaml +1 -0
- data/stdlib/nodejs/node_modules/js-yaml/node_modules/.bin/esparse +1 -0
- data/stdlib/nodejs/node_modules/js-yaml/node_modules/.bin/esvalidate +1 -0
- data/stdlib/nodejs/require.rb +1 -1
- data/stdlib/nodejs/yaml.rb +3 -2
- data/stdlib/opal-parser.rb +7 -2
- data/stdlib/pathname.rb +23 -1
- data/stdlib/phantomjs.rb +10 -0
- data/stdlib/promise.rb +38 -23
- data/tasks/building.rake +3 -3
- data/tasks/testing.rake +27 -14
- data/tasks/testing/mspec_special_calls.rb +1 -1
- data/tasks/testing/sprockets-phantomjs.js +4 -0
- data/test/opal/test_keyword.rb +110 -110
- data/test/opal/unsupported_and_bugs.rb +30 -0
- data/vendored-minitest/minitest/assertions.rb +1 -1
- metadata +65 -15
- data/.spectator +0 -2
- data/.spectator-mspec +0 -3
- data/opal/corelib/array/inheritance.rb +0 -127
- data/spec/rubyspecs +0 -139
@@ -0,0 +1,11 @@
|
|
1
|
+
class BigDecimal
|
2
|
+
%x{
|
3
|
+
var define = function (f) { Opal.casgn(self, 'BigNumber', f()); };
|
4
|
+
define.amd = true;
|
5
|
+
|
6
|
+
/* jshint ignore:start */
|
7
|
+
/* bignumber.js v2.1.4 https://github.com/MikeMcl/bignumber.js/LICENCE */
|
8
|
+
!function(e){"use strict";function n(e){function E(e,n){var t,r,i,o,u,s,f=this;if(!(f instanceof E))return j&&L(26,"constructor call without new",e),new E(e,n);if(null!=n&&H(n,2,64,M,"base")){if(n=0|n,s=e+"",10==n)return f=new E(e instanceof E?e:s),U(f,P+f.e+1,k);if((o="number"==typeof e)&&0*e!=0||!new RegExp("^-?"+(t="["+N.slice(0,n)+"]+")+"(?:\\."+t+")?$",37>n?"i":"").test(s))return h(f,s,o,n);o?(f.s=0>1/e?(s=s.slice(1),-1):1,j&&s.replace(/^0\.0*|\./,"").length>15&&L(M,v,e),o=!1):f.s=45===s.charCodeAt(0)?(s=s.slice(1),-1):1,s=D(s,10,n,f.s)}else{if(e instanceof E)return f.s=e.s,f.e=e.e,f.c=(e=e.c)?e.slice():e,void(M=0);if((o="number"==typeof e)&&0*e==0){if(f.s=0>1/e?(e=-e,-1):1,e===~~e){for(r=0,i=e;i>=10;i/=10,r++);return f.e=r,f.c=[e],void(M=0)}s=e+""}else{if(!g.test(s=e+""))return h(f,s,o);f.s=45===s.charCodeAt(0)?(s=s.slice(1),-1):1}}for((r=s.indexOf("."))>-1&&(s=s.replace(".","")),(i=s.search(/e/i))>0?(0>r&&(r=i),r+=+s.slice(i+1),s=s.substring(0,i)):0>r&&(r=s.length),i=0;48===s.charCodeAt(i);i++);for(u=s.length;48===s.charCodeAt(--u););if(s=s.slice(i,u+1))if(u=s.length,o&&j&&u>15&&L(M,v,f.s*e),r=r-i-1,r>z)f.c=f.e=null;else if(G>r)f.c=[f.e=0];else{if(f.e=r,f.c=[],i=(r+1)%O,0>r&&(i+=O),u>i){for(i&&f.c.push(+s.slice(0,i)),u-=O;u>i;)f.c.push(+s.slice(i,i+=O));s=s.slice(i),i=O-s.length}else i-=u;for(;i--;s+="0");f.c.push(+s)}else f.c=[f.e=0];M=0}function D(e,n,t,i){var o,u,f,c,a,h,g,p=e.indexOf("."),d=P,m=k;for(37>t&&(e=e.toLowerCase()),p>=0&&(f=J,J=0,e=e.replace(".",""),g=new E(t),a=g.pow(e.length-p),J=f,g.c=s(l(r(a.c),a.e),10,n),g.e=g.c.length),h=s(e,t,n),u=f=h.length;0==h[--f];h.pop());if(!h[0])return"0";if(0>p?--u:(a.c=h,a.e=u,a.s=i,a=C(a,g,d,m,n),h=a.c,c=a.r,u=a.e),o=u+d+1,p=h[o],f=n/2,c=c||0>o||null!=h[o+1],c=4>m?(null!=p||c)&&(0==m||m==(a.s<0?3:2)):p>f||p==f&&(4==m||c||6==m&&1&h[o-1]||m==(a.s<0?8:7)),1>o||!h[0])e=c?l("1",-d):"0";else{if(h.length=o,c)for(--n;++h[--o]>n;)h[o]=0,o||(++u,h.unshift(1));for(f=h.length;!h[--f];);for(p=0,e="";f>=p;e+=N.charAt(h[p++]));e=l(e,u)}return e}function F(e,n,t,i){var o,u,s,c,a;if(t=null!=t&&H(t,0,8,i,w)?0|t:k,!e.c)return e.toString();if(o=e.c[0],s=e.e,null==n)a=r(e.c),a=19==i||24==i&&B>=s?f(a,s):l(a,s);else if(e=U(new E(e),n,t),u=e.e,a=r(e.c),c=a.length,19==i||24==i&&(u>=n||B>=u)){for(;n>c;a+="0",c++);a=f(a,u)}else if(n-=s,a=l(a,u),u+1>c){if(--n>0)for(a+=".";n--;a+="0");}else if(n+=u-c,n>0)for(u+1==c&&(a+=".");n--;a+="0");return e.s<0&&o?"-"+a:a}function _(e,n){var t,r,i=0;for(u(e[0])&&(e=e[0]),t=new E(e[0]);++i<e.length;){if(r=new E(e[i]),!r.s){t=r;break}n.call(t,r)&&(t=r)}return t}function x(e,n,t,r,i){return(n>e||e>t||e!=c(e))&&L(r,(i||"decimal places")+(n>e||e>t?" out of range":" not an integer"),e),!0}function I(e,n,t){for(var r=1,i=n.length;!n[--i];n.pop());for(i=n[0];i>=10;i/=10,r++);return(t=r+t*O-1)>z?e.c=e.e=null:G>t?e.c=[e.e=0]:(e.e=t,e.c=n),e}function L(e,n,t){var r=new Error(["new BigNumber","cmp","config","div","divToInt","eq","gt","gte","lt","lte","minus","mod","plus","precision","random","round","shift","times","toDigits","toExponential","toFixed","toFormat","toFraction","pow","toPrecision","toString","BigNumber"][e]+"() "+n+": "+t);throw r.name="BigNumber Error",M=0,r}function U(e,n,t,r){var i,o,u,s,f,l,c,a=e.c,h=S;if(a){e:{for(i=1,s=a[0];s>=10;s/=10,i++);if(o=n-i,0>o)o+=O,u=n,f=a[l=0],c=f/h[i-u-1]%10|0;else if(l=p((o+1)/O),l>=a.length){if(!r)break e;for(;a.length<=l;a.push(0));f=c=0,i=1,o%=O,u=o-O+1}else{for(f=s=a[l],i=1;s>=10;s/=10,i++);o%=O,u=o-O+i,c=0>u?0:f/h[i-u-1]%10|0}if(r=r||0>n||null!=a[l+1]||(0>u?f:f%h[i-u-1]),r=4>t?(c||r)&&(0==t||t==(e.s<0?3:2)):c>5||5==c&&(4==t||r||6==t&&(o>0?u>0?f/h[i-u]:0:a[l-1])%10&1||t==(e.s<0?8:7)),1>n||!a[0])return a.length=0,r?(n-=e.e+1,a[0]=h[(O-n%O)%O],e.e=-n||0):a[0]=e.e=0,e;if(0==o?(a.length=l,s=1,l--):(a.length=l+1,s=h[O-o],a[l]=u>0?d(f/h[i-u]%h[u])*s:0),r)for(;;){if(0==l){for(o=1,u=a[0];u>=10;u/=10,o++);for(u=a[0]+=s,s=1;u>=10;u/=10,s++);o!=s&&(e.e++,a[0]==b&&(a[0]=1));break}if(a[l]+=s,a[l]!=b)break;a[l--]=0,s=1}for(o=a.length;0===a[--o];a.pop());}e.e>z?e.c=e.e=null:e.e<G&&(e.c=[e.e=0])}return e}var C,M=0,T=E.prototype,q=new E(1),P=20,k=4,B=-7,$=21,G=-1e7,z=1e7,j=!0,H=x,V=!1,W=1,J=100,X={decimalSeparator:".",groupSeparator:",",groupSize:3,secondaryGroupSize:0,fractionGroupSeparator:" ",fractionGroupSize:0};return E.another=n,E.ROUND_UP=0,E.ROUND_DOWN=1,E.ROUND_CEIL=2,E.ROUND_FLOOR=3,E.ROUND_HALF_UP=4,E.ROUND_HALF_DOWN=5,E.ROUND_HALF_EVEN=6,E.ROUND_HALF_CEIL=7,E.ROUND_HALF_FLOOR=8,E.EUCLID=9,E.config=function(){var e,n,t=0,r={},i=arguments,s=i[0],f=s&&"object"==typeof s?function(){return s.hasOwnProperty(n)?null!=(e=s[n]):void 0}:function(){return i.length>t?null!=(e=i[t++]):void 0};return f(n="DECIMAL_PLACES")&&H(e,0,A,2,n)&&(P=0|e),r[n]=P,f(n="ROUNDING_MODE")&&H(e,0,8,2,n)&&(k=0|e),r[n]=k,f(n="EXPONENTIAL_AT")&&(u(e)?H(e[0],-A,0,2,n)&&H(e[1],0,A,2,n)&&(B=0|e[0],$=0|e[1]):H(e,-A,A,2,n)&&(B=-($=0|(0>e?-e:e)))),r[n]=[B,$],f(n="RANGE")&&(u(e)?H(e[0],-A,-1,2,n)&&H(e[1],1,A,2,n)&&(G=0|e[0],z=0|e[1]):H(e,-A,A,2,n)&&(0|e?G=-(z=0|(0>e?-e:e)):j&&L(2,n+" cannot be zero",e))),r[n]=[G,z],f(n="ERRORS")&&(e===!!e||1===e||0===e?(M=0,H=(j=!!e)?x:o):j&&L(2,n+m,e)),r[n]=j,f(n="CRYPTO")&&(e===!!e||1===e||0===e?(V=!(!e||!a),e&&!V&&j&&L(2,"crypto unavailable",a)):j&&L(2,n+m,e)),r[n]=V,f(n="MODULO_MODE")&&H(e,0,9,2,n)&&(W=0|e),r[n]=W,f(n="POW_PRECISION")&&H(e,0,A,2,n)&&(J=0|e),r[n]=J,f(n="FORMAT")&&("object"==typeof e?X=e:j&&L(2,n+" not an object",e)),r[n]=X,r},E.max=function(){return _(arguments,T.lt)},E.min=function(){return _(arguments,T.gt)},E.random=function(){var e=9007199254740992,n=Math.random()*e&2097151?function(){return d(Math.random()*e)}:function(){return 8388608*(1073741824*Math.random()|0)+(8388608*Math.random()|0)};return function(e){var t,r,i,o,u,s=0,f=[],l=new E(q);if(e=null!=e&&H(e,0,A,14)?0|e:P,o=p(e/O),V)if(a&&a.getRandomValues){for(t=a.getRandomValues(new Uint32Array(o*=2));o>s;)u=131072*t[s]+(t[s+1]>>>11),u>=9e15?(r=a.getRandomValues(new Uint32Array(2)),t[s]=r[0],t[s+1]=r[1]):(f.push(u%1e14),s+=2);s=o/2}else if(a&&a.randomBytes){for(t=a.randomBytes(o*=7);o>s;)u=281474976710656*(31&t[s])+1099511627776*t[s+1]+4294967296*t[s+2]+16777216*t[s+3]+(t[s+4]<<16)+(t[s+5]<<8)+t[s+6],u>=9e15?a.randomBytes(7).copy(t,s):(f.push(u%1e14),s+=7);s=o/7}else j&&L(14,"crypto unavailable",a);if(!s)for(;o>s;)u=n(),9e15>u&&(f[s++]=u%1e14);for(o=f[--s],e%=O,o&&e&&(u=S[O-e],f[s]=d(o/u)*u);0===f[s];f.pop(),s--);if(0>s)f=[i=0];else{for(i=-1;0===f[0];f.shift(),i-=O);for(s=1,u=f[0];u>=10;u/=10,s++);O>s&&(i-=O-s)}return l.e=i,l.c=f,l}}(),C=function(){function e(e,n,t){var r,i,o,u,s=0,f=e.length,l=n%R,c=n/R|0;for(e=e.slice();f--;)o=e[f]%R,u=e[f]/R|0,r=c*o+u*l,i=l*o+r%R*R+s,s=(i/t|0)+(r/R|0)+c*u,e[f]=i%t;return s&&e.unshift(s),e}function n(e,n,t,r){var i,o;if(t!=r)o=t>r?1:-1;else for(i=o=0;t>i;i++)if(e[i]!=n[i]){o=e[i]>n[i]?1:-1;break}return o}function r(e,n,t,r){for(var i=0;t--;)e[t]-=i,i=e[t]<n[t]?1:0,e[t]=i*r+e[t]-n[t];for(;!e[0]&&e.length>1;e.shift());}return function(i,o,u,s,f){var l,c,a,h,g,p,m,w,v,N,y,S,R,A,D,F,_,x=i.s==o.s?1:-1,I=i.c,L=o.c;if(!(I&&I[0]&&L&&L[0]))return new E(i.s&&o.s&&(I?!L||I[0]!=L[0]:L)?I&&0==I[0]||!L?0*x:x/0:NaN);for(w=new E(x),v=w.c=[],c=i.e-o.e,x=u+c+1,f||(f=b,c=t(i.e/O)-t(o.e/O),x=x/O|0),a=0;L[a]==(I[a]||0);a++);if(L[a]>(I[a]||0)&&c--,0>x)v.push(1),h=!0;else{for(A=I.length,F=L.length,a=0,x+=2,g=d(f/(L[0]+1)),g>1&&(L=e(L,g,f),I=e(I,g,f),F=L.length,A=I.length),R=F,N=I.slice(0,F),y=N.length;F>y;N[y++]=0);_=L.slice(),_.unshift(0),D=L[0],L[1]>=f/2&&D++;do{if(g=0,l=n(L,N,F,y),0>l){if(S=N[0],F!=y&&(S=S*f+(N[1]||0)),g=d(S/D),g>1)for(g>=f&&(g=f-1),p=e(L,g,f),m=p.length,y=N.length;1==n(p,N,m,y);)g--,r(p,m>F?_:L,m,f),m=p.length,l=1;else 0==g&&(l=g=1),p=L.slice(),m=p.length;if(y>m&&p.unshift(0),r(N,p,y,f),y=N.length,-1==l)for(;n(L,N,F,y)<1;)g++,r(N,y>F?_:L,y,f),y=N.length}else 0===l&&(g++,N=[0]);v[a++]=g,N[0]?N[y++]=I[R]||0:(N=[I[R]],y=1)}while((R++<A||null!=N[0])&&x--);h=null!=N[0],v[0]||v.shift()}if(f==b){for(a=1,x=v[0];x>=10;x/=10,a++);U(w,u+(w.e=a+c*O-1)+1,s,h)}else w.e=c,w.r=+h;return w}}(),h=function(){var e=/^(-?)0([xbo])(?=\w[\w.]*$)/i,n=/^([^.]+)\.$/,t=/^\.([^.]+)$/,r=/^-?(Infinity|NaN)$/,i=/^\s*\+(?=[\w.])|^\s+|\s+$/g;return function(o,u,s,f){var l,c=s?u:u.replace(i,"");if(r.test(c))o.s=isNaN(c)?null:0>c?-1:1;else{if(!s&&(c=c.replace(e,function(e,n,t){return l="x"==(t=t.toLowerCase())?16:"b"==t?2:8,f&&f!=l?e:n}),f&&(l=f,c=c.replace(n,"$1").replace(t,"0.$1")),u!=c))return new E(c,l);j&&L(M,"not a"+(f?" base "+f:"")+" number",u),o.s=null}o.c=o.e=null,M=0}}(),T.absoluteValue=T.abs=function(){var e=new E(this);return e.s<0&&(e.s=1),e},T.ceil=function(){return U(new E(this),this.e+1,2)},T.comparedTo=T.cmp=function(e,n){return M=1,i(this,new E(e,n))},T.decimalPlaces=T.dp=function(){var e,n,r=this.c;if(!r)return null;if(e=((n=r.length-1)-t(this.e/O))*O,n=r[n])for(;n%10==0;n/=10,e--);return 0>e&&(e=0),e},T.dividedBy=T.div=function(e,n){return M=3,C(this,new E(e,n),P,k)},T.dividedToIntegerBy=T.divToInt=function(e,n){return M=4,C(this,new E(e,n),0,1)},T.equals=T.eq=function(e,n){return M=5,0===i(this,new E(e,n))},T.floor=function(){return U(new E(this),this.e+1,3)},T.greaterThan=T.gt=function(e,n){return M=6,i(this,new E(e,n))>0},T.greaterThanOrEqualTo=T.gte=function(e,n){return M=7,1===(n=i(this,new E(e,n)))||0===n},T.isFinite=function(){return!!this.c},T.isInteger=T.isInt=function(){return!!this.c&&t(this.e/O)>this.c.length-2},T.isNaN=function(){return!this.s},T.isNegative=T.isNeg=function(){return this.s<0},T.isZero=function(){return!!this.c&&0==this.c[0]},T.lessThan=T.lt=function(e,n){return M=8,i(this,new E(e,n))<0},T.lessThanOrEqualTo=T.lte=function(e,n){return M=9,-1===(n=i(this,new E(e,n)))||0===n},T.minus=T.sub=function(e,n){var r,i,o,u,s=this,f=s.s;if(M=10,e=new E(e,n),n=e.s,!f||!n)return new E(NaN);if(f!=n)return e.s=-n,s.plus(e);var l=s.e/O,c=e.e/O,a=s.c,h=e.c;if(!l||!c){if(!a||!h)return a?(e.s=-n,e):new E(h?s:NaN);if(!a[0]||!h[0])return h[0]?(e.s=-n,e):new E(a[0]?s:3==k?-0:0)}if(l=t(l),c=t(c),a=a.slice(),f=l-c){for((u=0>f)?(f=-f,o=a):(c=l,o=h),o.reverse(),n=f;n--;o.push(0));o.reverse()}else for(i=(u=(f=a.length)<(n=h.length))?f:n,f=n=0;i>n;n++)if(a[n]!=h[n]){u=a[n]<h[n];break}if(u&&(o=a,a=h,h=o,e.s=-e.s),n=(i=h.length)-(r=a.length),n>0)for(;n--;a[r++]=0);for(n=b-1;i>f;){if(a[--i]<h[i]){for(r=i;r&&!a[--r];a[r]=n);--a[r],a[i]+=b}a[i]-=h[i]}for(;0==a[0];a.shift(),--c);return a[0]?I(e,a,c):(e.s=3==k?-1:1,e.c=[e.e=0],e)},T.modulo=T.mod=function(e,n){var t,r,i=this;return M=11,e=new E(e,n),!i.c||!e.s||e.c&&!e.c[0]?new E(NaN):!e.c||i.c&&!i.c[0]?new E(i):(9==W?(r=e.s,e.s=1,t=C(i,e,0,3),e.s=r,t.s*=r):t=C(i,e,0,W),i.minus(t.times(e)))},T.negated=T.neg=function(){var e=new E(this);return e.s=-e.s||null,e},T.plus=T.add=function(e,n){var r,i=this,o=i.s;if(M=12,e=new E(e,n),n=e.s,!o||!n)return new E(NaN);if(o!=n)return e.s=-n,i.minus(e);var u=i.e/O,s=e.e/O,f=i.c,l=e.c;if(!u||!s){if(!f||!l)return new E(o/0);if(!f[0]||!l[0])return l[0]?e:new E(f[0]?i:0*o)}if(u=t(u),s=t(s),f=f.slice(),o=u-s){for(o>0?(s=u,r=l):(o=-o,r=f),r.reverse();o--;r.push(0));r.reverse()}for(o=f.length,n=l.length,0>o-n&&(r=l,l=f,f=r,n=o),o=0;n;)o=(f[--n]=f[n]+l[n]+o)/b|0,f[n]%=b;return o&&(f.unshift(o),++s),I(e,f,s)},T.precision=T.sd=function(e){var n,t,r=this,i=r.c;if(null!=e&&e!==!!e&&1!==e&&0!==e&&(j&&L(13,"argument"+m,e),e!=!!e&&(e=null)),!i)return null;if(t=i.length-1,n=t*O+1,t=i[t]){for(;t%10==0;t/=10,n--);for(t=i[0];t>=10;t/=10,n++);}return e&&r.e+1>n&&(n=r.e+1),n},T.round=function(e,n){var t=new E(this);return(null==e||H(e,0,A,15))&&U(t,~~e+this.e+1,null!=n&&H(n,0,8,15,w)?0|n:k),t},T.shift=function(e){var n=this;return H(e,-y,y,16,"argument")?n.times("1e"+c(e)):new E(n.c&&n.c[0]&&(-y>e||e>y)?n.s*(0>e?0:1/0):n)},T.squareRoot=T.sqrt=function(){var e,n,i,o,u,s=this,f=s.c,l=s.s,c=s.e,a=P+4,h=new E("0.5");if(1!==l||!f||!f[0])return new E(!l||0>l&&(!f||f[0])?NaN:f?s:1/0);if(l=Math.sqrt(+s),0==l||l==1/0?(n=r(f),(n.length+c)%2==0&&(n+="0"),l=Math.sqrt(n),c=t((c+1)/2)-(0>c||c%2),l==1/0?n="1e"+c:(n=l.toExponential(),n=n.slice(0,n.indexOf("e")+1)+c),i=new E(n)):i=new E(l+""),i.c[0])for(c=i.e,l=c+a,3>l&&(l=0);;)if(u=i,i=h.times(u.plus(C(s,u,a,1))),r(u.c).slice(0,l)===(n=r(i.c)).slice(0,l)){if(i.e<c&&--l,n=n.slice(l-3,l+1),"9999"!=n&&(o||"4999"!=n)){(!+n||!+n.slice(1)&&"5"==n.charAt(0))&&(U(i,i.e+P+2,1),e=!i.times(i).eq(s));break}if(!o&&(U(u,u.e+P+2,0),u.times(u).eq(s))){i=u;break}a+=4,l+=4,o=1}return U(i,i.e+P+1,k,e)},T.times=T.mul=function(e,n){var r,i,o,u,s,f,l,c,a,h,g,p,d,m,w,v=this,N=v.c,y=(M=17,e=new E(e,n)).c;if(!(N&&y&&N[0]&&y[0]))return!v.s||!e.s||N&&!N[0]&&!y||y&&!y[0]&&!N?e.c=e.e=e.s=null:(e.s*=v.s,N&&y?(e.c=[0],e.e=0):e.c=e.e=null),e;for(i=t(v.e/O)+t(e.e/O),e.s*=v.s,l=N.length,h=y.length,h>l&&(d=N,N=y,y=d,o=l,l=h,h=o),o=l+h,d=[];o--;d.push(0));for(m=b,w=R,o=h;--o>=0;){for(r=0,g=y[o]%w,p=y[o]/w|0,s=l,u=o+s;u>o;)c=N[--s]%w,a=N[s]/w|0,f=p*c+a*g,c=g*c+f%w*w+d[u]+r,r=(c/m|0)+(f/w|0)+p*a,d[u--]=c%m;d[u]=r}return r?++i:d.shift(),I(e,d,i)},T.toDigits=function(e,n){var t=new E(this);return e=null!=e&&H(e,1,A,18,"precision")?0|e:null,n=null!=n&&H(n,0,8,18,w)?0|n:k,e?U(t,e,n):t},T.toExponential=function(e,n){return F(this,null!=e&&H(e,0,A,19)?~~e+1:null,n,19)},T.toFixed=function(e,n){return F(this,null!=e&&H(e,0,A,20)?~~e+this.e+1:null,n,20)},T.toFormat=function(e,n){var t=F(this,null!=e&&H(e,0,A,21)?~~e+this.e+1:null,n,21);if(this.c){var r,i=t.split("."),o=+X.groupSize,u=+X.secondaryGroupSize,s=X.groupSeparator,f=i[0],l=i[1],c=this.s<0,a=c?f.slice(1):f,h=a.length;if(u&&(r=o,o=u,u=r,h-=r),o>0&&h>0){for(r=h%o||o,f=a.substr(0,r);h>r;r+=o)f+=s+a.substr(r,o);u>0&&(f+=s+a.slice(r)),c&&(f="-"+f)}t=l?f+X.decimalSeparator+((u=+X.fractionGroupSize)?l.replace(new RegExp("\\d{"+u+"}\\B","g"),"$&"+X.fractionGroupSeparator):l):f}return t},T.toFraction=function(e){var n,t,i,o,u,s,f,l,c,a=j,h=this,g=h.c,p=new E(q),d=t=new E(q),m=f=new E(q);if(null!=e&&(j=!1,s=new E(e),j=a,(!(a=s.isInt())||s.lt(q))&&(j&&L(22,"max denominator "+(a?"out of range":"not an integer"),e),e=!a&&s.c&&U(s,s.e+1,1).gte(q)?s:null)),!g)return h.toString();for(c=r(g),o=p.e=c.length-h.e-1,p.c[0]=S[(u=o%O)<0?O+u:u],e=!e||s.cmp(p)>0?o>0?p:d:s,u=z,z=1/0,s=new E(c),f.c[0]=0;l=C(s,p,0,1),i=t.plus(l.times(m)),1!=i.cmp(e);)t=m,m=i,d=f.plus(l.times(i=d)),f=i,p=s.minus(l.times(i=p)),s=i;return i=C(e.minus(t),m,0,1),f=f.plus(i.times(d)),t=t.plus(i.times(m)),f.s=d.s=h.s,o*=2,n=C(d,m,o,k).minus(h).abs().cmp(C(f,t,o,k).minus(h).abs())<1?[d.toString(),m.toString()]:[f.toString(),t.toString()],z=u,n},T.toNumber=function(){return+this},T.toPower=T.pow=function(e){var n,t,r=d(0>e?-e:+e),i=this;if(!H(e,-y,y,23,"exponent")&&(!isFinite(e)||r>y&&(e/=0)||parseFloat(e)!=e&&!(e=NaN)))return new E(Math.pow(+i,e));for(n=J?p(J/O+2):0,t=new E(q);;){if(r%2){if(t=t.times(i),!t.c)break;n&&t.c.length>n&&(t.c.length=n)}if(r=d(r/2),!r)break;i=i.times(i),n&&i.c&&i.c.length>n&&(i.c.length=n)}return 0>e&&(t=q.div(t)),n?U(t,J,k):t},T.toPrecision=function(e,n){return F(this,null!=e&&H(e,1,A,24,"precision")?0|e:null,n,24)},T.toString=function(e){var n,t=this,i=t.s,o=t.e;return null===o?i?(n="Infinity",0>i&&(n="-"+n)):n="NaN":(n=r(t.c),n=null!=e&&H(e,2,64,25,"base")?D(l(n,o),0|e,10,i):B>=o||o>=$?f(n,o):l(n,o),0>i&&t.c[0]&&(n="-"+n)),n},T.truncated=T.trunc=function(){return U(new E(this),this.e+1,1)},T.valueOf=T.toJSON=function(){var e,n=this,t=n.e;return null===t?n.toString():(e=r(n.c),e=B>=t||t>=$?f(e,t):l(e,t),n.s<0?"-"+e:e)},null!=e&&E.config(e),E}function t(e){var n=0|e;return e>0||e===n?n:n-1}function r(e){for(var n,t,r=1,i=e.length,o=e[0]+"";i>r;){for(n=e[r++]+"",t=O-n.length;t--;n="0"+n);o+=n}for(i=o.length;48===o.charCodeAt(--i););return o.slice(0,i+1||1)}function i(e,n){var t,r,i=e.c,o=n.c,u=e.s,s=n.s,f=e.e,l=n.e;if(!u||!s)return null;if(t=i&&!i[0],r=o&&!o[0],t||r)return t?r?0:-s:u;if(u!=s)return u;if(t=0>u,r=f==l,!i||!o)return r?0:!i^t?1:-1;if(!r)return f>l^t?1:-1;for(s=(f=i.length)<(l=o.length)?f:l,u=0;s>u;u++)if(i[u]!=o[u])return i[u]>o[u]^t?1:-1;return f==l?0:f>l^t?1:-1}function o(e,n,t){return(e=c(e))>=n&&t>=e}function u(e){return"[object Array]"==Object.prototype.toString.call(e)}function s(e,n,t){for(var r,i,o=[0],u=0,s=e.length;s>u;){for(i=o.length;i--;o[i]*=n);for(o[r=0]+=N.indexOf(e.charAt(u++));r<o.length;r++)o[r]>t-1&&(null==o[r+1]&&(o[r+1]=0),o[r+1]+=o[r]/t|0,o[r]%=t)}return o.reverse()}function f(e,n){return(e.length>1?e.charAt(0)+"."+e.slice(1):e)+(0>n?"e":"e+")+n}function l(e,n){var t,r;if(0>n){for(r="0.";++n;r+="0");e=r+e}else if(t=e.length,++n>t){for(r="0",n-=t;--n;r+="0");e+=r}else t>n&&(e=e.slice(0,n)+"."+e.slice(n));return e}function c(e){return e=parseFloat(e),0>e?p(e):d(e)}var a,h,g=/^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,p=Math.ceil,d=Math.floor,m=" not a boolean or binary digit",w="rounding mode",v="number type has more than 15 significant digits",N="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_",b=1e14,O=14,y=9007199254740991,S=[1,10,100,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13],R=1e7,A=1e9;if("undefined"!=typeof crypto&&(a=crypto),"function"==typeof define&&define.amd)define(function(){return n()});else if("undefined"!=typeof module&&module.exports){if(module.exports=n(),!a)try{a=require("crypto")}catch(E){}}else e||(e="undefined"!=typeof self?self:Function("return this")()),e.BigNumber=n()}(this);
|
9
|
+
/* jshint ignore:end */
|
10
|
+
}
|
11
|
+
end
|
data/stdlib/date.rb
CHANGED
@@ -71,13 +71,14 @@ class Date
|
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
74
|
-
JULIAN
|
75
|
-
GREGORIAN
|
76
|
-
ITALY
|
77
|
-
ENGLAND
|
78
|
-
MONTHNAMES
|
79
|
-
|
80
|
-
|
74
|
+
JULIAN = Infinity.new
|
75
|
+
GREGORIAN = -Infinity.new
|
76
|
+
ITALY = 2299161 # 1582-10-15
|
77
|
+
ENGLAND = 2361222 # 1752-09-14
|
78
|
+
MONTHNAMES = [nil] + %w(January February March April May June July August September October November December)
|
79
|
+
ABBR_MONTHNAMES = %w(jan feb mar apr may jun jul aug sep oct nov dec)
|
80
|
+
DAYNAMES = %w(Sunday Monday Tuesday Wednesday Thursday Friday Saturday)
|
81
|
+
ABBR_DAYNAMES = %w(Sun Mon Tue Wed Thu Fri Sat)
|
81
82
|
|
82
83
|
class << self
|
83
84
|
alias civil new
|
@@ -88,9 +89,250 @@ class Date
|
|
88
89
|
instance
|
89
90
|
end
|
90
91
|
|
91
|
-
def parse(string)
|
92
|
-
|
93
|
-
|
92
|
+
def parse(string, comp = true)
|
93
|
+
%x{
|
94
|
+
var current_date = new Date();
|
95
|
+
|
96
|
+
var current_day = current_date.getDate(),
|
97
|
+
current_month = current_date.getMonth(),
|
98
|
+
current_year = current_date.getFullYear(),
|
99
|
+
current_wday = current_date.getDay(),
|
100
|
+
full_month_name_regexp = #{MONTHNAMES.compact.join("|")};
|
101
|
+
|
102
|
+
function match1(match) { return match[1]; }
|
103
|
+
function match2(match) { return match[2]; }
|
104
|
+
function match3(match) { return match[3]; }
|
105
|
+
function match4(match) { return match[4]; }
|
106
|
+
|
107
|
+
// Converts passed short year (0..99)
|
108
|
+
// to a 4-digits year in the range (1969..2068)
|
109
|
+
function fromShortYear(fn) {
|
110
|
+
return function(match) {
|
111
|
+
var short_year = fn(match);
|
112
|
+
|
113
|
+
if (short_year >= 69) {
|
114
|
+
short_year += 1900;
|
115
|
+
} else {
|
116
|
+
short_year += 2000;
|
117
|
+
}
|
118
|
+
return short_year;
|
119
|
+
}
|
120
|
+
}
|
121
|
+
|
122
|
+
// Converts month abbr (nov) to a month number
|
123
|
+
function fromMonthAbbr(fn) {
|
124
|
+
return function(match) {
|
125
|
+
var abbr = fn(match);
|
126
|
+
return #{ABBR_MONTHNAMES}.indexOf(abbr) + 1;
|
127
|
+
}
|
128
|
+
}
|
129
|
+
|
130
|
+
function toInt(fn) {
|
131
|
+
return function(match) {
|
132
|
+
var value = fn(match);
|
133
|
+
return parseInt(value, 10);
|
134
|
+
}
|
135
|
+
}
|
136
|
+
|
137
|
+
// Depending on the 'comp' value appends 20xx to a passed year
|
138
|
+
function to2000(fn) {
|
139
|
+
return function(match) {
|
140
|
+
var value = fn(match);
|
141
|
+
if (comp) {
|
142
|
+
return value + 2000;
|
143
|
+
} else {
|
144
|
+
return value;
|
145
|
+
}
|
146
|
+
}
|
147
|
+
}
|
148
|
+
|
149
|
+
// Converts passed week day name to a day number
|
150
|
+
function fromDayName(fn) {
|
151
|
+
return function(match) {
|
152
|
+
var dayname = fn(match),
|
153
|
+
wday = #{DAYNAMES.map(&:downcase)}.indexOf(#{`dayname`.downcase});
|
154
|
+
|
155
|
+
return current_day - current_wday + wday;
|
156
|
+
}
|
157
|
+
}
|
158
|
+
|
159
|
+
// Converts passed month name to a month number
|
160
|
+
function fromFullMonthName(fn) {
|
161
|
+
return function(match) {
|
162
|
+
var month_name = fn(match);
|
163
|
+
return #{MONTHNAMES.compact.map(&:downcase)}.indexOf(#{`month_name`.downcase}) + 1;
|
164
|
+
}
|
165
|
+
}
|
166
|
+
|
167
|
+
var rules = [
|
168
|
+
{
|
169
|
+
// DD as month day number
|
170
|
+
regexp: /^(\d{2})$/,
|
171
|
+
year: current_year,
|
172
|
+
month: current_month,
|
173
|
+
day: toInt(match1)
|
174
|
+
},
|
175
|
+
{
|
176
|
+
// DDD as year day number
|
177
|
+
regexp: /^(\d{3})$/,
|
178
|
+
year: current_year,
|
179
|
+
month: 0,
|
180
|
+
day: toInt(match1)
|
181
|
+
},
|
182
|
+
{
|
183
|
+
// MMDD as month and day
|
184
|
+
regexp: /^(\d{2})(\d{2})$/,
|
185
|
+
year: current_year,
|
186
|
+
month: toInt(match1),
|
187
|
+
day: toInt(match2)
|
188
|
+
},
|
189
|
+
{
|
190
|
+
// YYDDD as year and day number in 1969--2068
|
191
|
+
regexp: /^(\d{2})(\d{3})$/,
|
192
|
+
year: fromShortYear(toInt(match1)),
|
193
|
+
month: 0,
|
194
|
+
day: toInt(match2)
|
195
|
+
},
|
196
|
+
{
|
197
|
+
// YYMMDD as year, month and day in 1969--2068
|
198
|
+
regexp: /^(\d{2})(\d{2})(\d{2})$/,
|
199
|
+
year: fromShortYear(toInt(match1)),
|
200
|
+
month: toInt(match2),
|
201
|
+
day: toInt(match3)
|
202
|
+
},
|
203
|
+
{
|
204
|
+
// YYYYDDD as year and day number
|
205
|
+
regexp: /^(\d{4})(\d{3})$/,
|
206
|
+
year: toInt(match1),
|
207
|
+
month: 0,
|
208
|
+
day: toInt(match2)
|
209
|
+
},
|
210
|
+
{
|
211
|
+
// YYYYMMDD as year, month and day number
|
212
|
+
regexp: /^(\d{4})(\d{2})(\d{2})$/,
|
213
|
+
year: toInt(match1),
|
214
|
+
month: toInt(match2),
|
215
|
+
day: toInt(match3)
|
216
|
+
},
|
217
|
+
{
|
218
|
+
// mmm YYYY
|
219
|
+
regexp: /^([a-z]{3})[\s\.\/\-](\d{3,4})$/,
|
220
|
+
year: toInt(match2),
|
221
|
+
month: fromMonthAbbr(match1),
|
222
|
+
day: 1
|
223
|
+
},
|
224
|
+
{
|
225
|
+
// DD mmm YYYY
|
226
|
+
regexp: /^(\d{1,2})[\s\.\/\-]([a-z]{3})[\s\.\/\-](\d{3,4})$/i,
|
227
|
+
year: toInt(match3),
|
228
|
+
month: fromMonthAbbr(match2),
|
229
|
+
day: toInt(match1)
|
230
|
+
},
|
231
|
+
{
|
232
|
+
// mmm DD YYYY
|
233
|
+
regexp: /^([a-z]{3})[\s\.\/\-](\d{1,2})[\s\.\/\-](\d{3,4})$/i,
|
234
|
+
year: toInt(match3),
|
235
|
+
month: fromMonthAbbr(match1),
|
236
|
+
day: toInt(match2)
|
237
|
+
},
|
238
|
+
{
|
239
|
+
// YYYY mmm DD
|
240
|
+
regexp: /^(\d{3,4})[\s\.\/\-]([a-z]{3})[\s\.\/\-](\d{1,2})$/i,
|
241
|
+
year: toInt(match1),
|
242
|
+
month: fromMonthAbbr(match2),
|
243
|
+
day: toInt(match3)
|
244
|
+
},
|
245
|
+
{
|
246
|
+
// YYYY-MM-DD YYYY/MM/DD YYYY.MM.DD
|
247
|
+
regexp: /^(\-?\d{3,4})[\s\.\/\-](\d{1,2})[\s\.\/\-](\d{1,2})$/,
|
248
|
+
year: toInt(match1),
|
249
|
+
month: toInt(match2),
|
250
|
+
day: toInt(match3)
|
251
|
+
},
|
252
|
+
{
|
253
|
+
// YY-MM-DD
|
254
|
+
regexp: /^(\d{2})[\s\.\/\-](\d{1,2})[\s\.\/\-](\d{1,2})$/,
|
255
|
+
year: to2000(toInt(match1)),
|
256
|
+
month: toInt(match2),
|
257
|
+
day: toInt(match3)
|
258
|
+
},
|
259
|
+
{
|
260
|
+
// DD-MM-YYYY
|
261
|
+
regexp: /^(\d{1,2})[\s\.\/\-](\d{1,2})[\s\.\/\-](\-?\d{3,4})$/,
|
262
|
+
year: toInt(match3),
|
263
|
+
month: toInt(match2),
|
264
|
+
day: toInt(match1)
|
265
|
+
},
|
266
|
+
{
|
267
|
+
// ddd
|
268
|
+
regexp: new RegExp("^(" + #{DAYNAMES.join("|")} + ")$", 'i'),
|
269
|
+
year: current_year,
|
270
|
+
month: current_month,
|
271
|
+
day: fromDayName(match1)
|
272
|
+
},
|
273
|
+
{
|
274
|
+
// monthname daynumber YYYY
|
275
|
+
regexp: new RegExp("^(" + full_month_name_regexp + ")[\\s\\.\\/\\-](\\d{1,2})(th|nd|rd)[\\s\\.\\/\\-](\\-?\\d{3,4})$", "i"),
|
276
|
+
year: toInt(match4),
|
277
|
+
month: fromFullMonthName(match1),
|
278
|
+
day: toInt(match2)
|
279
|
+
},
|
280
|
+
{
|
281
|
+
// monthname daynumber
|
282
|
+
regexp: new RegExp("^(" + full_month_name_regexp + ")[\\s\\.\\/\\-](\\d{1,2})(th|nd|rd)", "i"),
|
283
|
+
year: current_year,
|
284
|
+
month: fromFullMonthName(match1),
|
285
|
+
day: toInt(match2)
|
286
|
+
},
|
287
|
+
{
|
288
|
+
// daynumber monthname YYYY
|
289
|
+
regexp: new RegExp("^(\\d{1,2})(th|nd|rd)[\\s\\.\\/\\-](" + full_month_name_regexp + ")[\\s\\.\\/\\-](\\-?\\d{3,4})$", "i"),
|
290
|
+
year: toInt(match4),
|
291
|
+
month: fromFullMonthName(match3),
|
292
|
+
day: toInt(match1)
|
293
|
+
},
|
294
|
+
{
|
295
|
+
// YYYY monthname daynumber
|
296
|
+
regexp: new RegExp("^(\\-?\\d{3,4})[\\s\\.\\/\\-](" + full_month_name_regexp + ")[\\s\\.\\/\\-](\\d{1,2})(th|nd|rd)$", "i"),
|
297
|
+
year: toInt(match1),
|
298
|
+
month: fromFullMonthName(match2),
|
299
|
+
day: toInt(match3)
|
300
|
+
}
|
301
|
+
]
|
302
|
+
|
303
|
+
var rule, i, match;
|
304
|
+
|
305
|
+
for (i = 0; i < rules.length; i++) {
|
306
|
+
rule = rules[i];
|
307
|
+
match = rule.regexp.exec(string);
|
308
|
+
if (match) {
|
309
|
+
var year = rule.year;
|
310
|
+
if (typeof(year) === 'function') {
|
311
|
+
year = year(match);
|
312
|
+
}
|
313
|
+
|
314
|
+
var month = rule.month;
|
315
|
+
if (typeof(month) === 'function') {
|
316
|
+
month = month(match) - 1
|
317
|
+
}
|
318
|
+
|
319
|
+
var day = rule.day;
|
320
|
+
if (typeof(day) === 'function') {
|
321
|
+
day = day(match);
|
322
|
+
}
|
323
|
+
|
324
|
+
var result = new Date(year, month, day);
|
325
|
+
|
326
|
+
// an edge case, JS can't handle 'new Date(1)', minimal year is 1970
|
327
|
+
if (year >= 0 && year <= 1970) {
|
328
|
+
result.setFullYear(year);
|
329
|
+
}
|
330
|
+
|
331
|
+
return #{wrap `result`};
|
332
|
+
}
|
333
|
+
}
|
334
|
+
}
|
335
|
+
raise ArgumentError, 'invalid date'
|
94
336
|
end
|
95
337
|
|
96
338
|
def today
|
data/stdlib/native.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
#
|
1
|
+
# Provides a complete set of tools to wrap native JavaScript
|
2
2
|
# into nice Ruby objects.
|
3
3
|
#
|
4
|
-
#
|
4
|
+
# @example
|
5
5
|
#
|
6
6
|
# $$.document.querySelector('p').classList.add('blue')
|
7
7
|
# # => adds "blue" class to <p>
|
@@ -24,7 +24,7 @@ module Native
|
|
24
24
|
}
|
25
25
|
end
|
26
26
|
|
27
|
-
def self.try_convert(value)
|
27
|
+
def self.try_convert(value, default=nil)
|
28
28
|
%x{
|
29
29
|
if (#{native?(value)}) {
|
30
30
|
return #{value};
|
@@ -33,7 +33,7 @@ module Native
|
|
33
33
|
return #{value.to_n};
|
34
34
|
}
|
35
35
|
else {
|
36
|
-
return
|
36
|
+
return #{default};
|
37
37
|
}
|
38
38
|
}
|
39
39
|
end
|
@@ -105,18 +105,23 @@ module Native
|
|
105
105
|
end
|
106
106
|
|
107
107
|
module Helpers
|
108
|
-
#
|
108
|
+
# Exposes a native JavaScript method to Ruby
|
109
109
|
#
|
110
110
|
#
|
111
|
-
# new
|
112
|
-
#
|
111
|
+
# @param new [String]
|
112
|
+
# The name of the newly created method.
|
113
|
+
#
|
114
|
+
# @param old [String]
|
115
|
+
# The name of the native JavaScript method to be exposed.
|
113
116
|
# If the name ends with "=" (e.g. `foo=`) it will be interpreted as
|
114
117
|
# a property setter. (default: the value of "new")
|
115
|
-
#
|
118
|
+
#
|
119
|
+
# @param as [Class]
|
120
|
+
# If provided the values returned by the original method will be
|
116
121
|
# returned as instances of the passed class. The class passed to "as"
|
117
122
|
# is expected to accept a native JavaScript value.
|
118
123
|
#
|
119
|
-
#
|
124
|
+
# @example
|
120
125
|
#
|
121
126
|
# class Element
|
122
127
|
# include Native::Helpers
|
@@ -191,7 +196,7 @@ module Native
|
|
191
196
|
@native = native
|
192
197
|
end
|
193
198
|
|
194
|
-
#
|
199
|
+
# Returns the internal native JavaScript value
|
195
200
|
def to_n
|
196
201
|
@native
|
197
202
|
end
|
@@ -202,12 +207,11 @@ module Kernel
|
|
202
207
|
`value == null || !value.$$class`
|
203
208
|
end
|
204
209
|
|
205
|
-
#
|
210
|
+
# Wraps a native JavaScript with `Native::Object.new`
|
206
211
|
#
|
207
|
-
#
|
208
|
-
#
|
209
|
-
#
|
210
|
-
# 3. The object itself if it's not native
|
212
|
+
# @return [Native::Object] The wrapped object if it is native
|
213
|
+
# @return [nil] for `null` and `undefined`
|
214
|
+
# @return [obj] The object itself if it's not native
|
211
215
|
def Native(obj)
|
212
216
|
if `#{obj} == null`
|
213
217
|
nil
|
@@ -228,7 +232,7 @@ module Kernel
|
|
228
232
|
|
229
233
|
alias_method :_Array, :Array
|
230
234
|
|
231
|
-
#
|
235
|
+
# Wraps array-like JavaScript objects in Native::Array
|
232
236
|
def Array(object, *args, &block)
|
233
237
|
if native?(object)
|
234
238
|
return Native::Array.new(object, *args, &block).to_a
|
@@ -370,11 +374,7 @@ class Native::Array
|
|
370
374
|
|
371
375
|
%x{
|
372
376
|
for (var i = 0, length = #{length}; i < length; i++) {
|
373
|
-
|
374
|
-
|
375
|
-
if (value === $breaker) {
|
376
|
-
return $breaker.$v;
|
377
|
-
}
|
377
|
+
Opal.yield1(block, #{self[`i`]});
|
378
378
|
}
|
379
379
|
}
|
380
380
|
|
@@ -435,48 +435,48 @@ class Native::Array
|
|
435
435
|
end
|
436
436
|
|
437
437
|
class Numeric
|
438
|
-
#
|
438
|
+
# @return the internal JavaScript value (with `valueOf`).
|
439
439
|
def to_n
|
440
440
|
`self.valueOf()`
|
441
441
|
end
|
442
442
|
end
|
443
443
|
|
444
444
|
class Proc
|
445
|
-
#
|
445
|
+
# @return itself (an instance of `Function`)
|
446
446
|
def to_n
|
447
447
|
self
|
448
448
|
end
|
449
449
|
end
|
450
450
|
|
451
451
|
class String
|
452
|
-
#
|
452
|
+
# @return the internal JavaScript value (with `valueOf`).
|
453
453
|
def to_n
|
454
454
|
`self.valueOf()`
|
455
455
|
end
|
456
456
|
end
|
457
457
|
|
458
458
|
class Regexp
|
459
|
-
#
|
459
|
+
# @return the internal JavaScript value (with `valueOf`).
|
460
460
|
def to_n
|
461
461
|
`self.valueOf()`
|
462
462
|
end
|
463
463
|
end
|
464
464
|
|
465
465
|
class MatchData
|
466
|
-
#
|
466
|
+
# @return the array of matches
|
467
467
|
def to_n
|
468
468
|
@matches
|
469
469
|
end
|
470
470
|
end
|
471
471
|
|
472
472
|
class Struct
|
473
|
-
#
|
473
|
+
# @return a JavaScript object with the members as keys and their
|
474
474
|
# values as values.
|
475
475
|
def to_n
|
476
476
|
result = `{}`
|
477
477
|
|
478
478
|
each_pair {|name, value|
|
479
|
-
`#{result}[#{name}] = #{Native.try_convert(value)}`
|
479
|
+
`#{result}[#{name}] = #{Native.try_convert(value, value)}`
|
480
480
|
}
|
481
481
|
|
482
482
|
result
|
@@ -484,7 +484,7 @@ class Struct
|
|
484
484
|
end
|
485
485
|
|
486
486
|
class Array
|
487
|
-
#
|
487
|
+
# Retuns a copy of itself trying to call #to_n on each member.
|
488
488
|
def to_n
|
489
489
|
%x{
|
490
490
|
var result = [];
|
@@ -492,7 +492,7 @@ class Array
|
|
492
492
|
for (var i = 0, length = self.length; i < length; i++) {
|
493
493
|
var obj = self[i];
|
494
494
|
|
495
|
-
result.push(#{Native.try_convert(`obj`)});
|
495
|
+
result.push(#{Native.try_convert(`obj`, `obj`)});
|
496
496
|
}
|
497
497
|
|
498
498
|
return result;
|
@@ -501,21 +501,21 @@ class Array
|
|
501
501
|
end
|
502
502
|
|
503
503
|
class Boolean
|
504
|
-
#
|
504
|
+
# @return the internal JavaScript value (with `valueOf`).
|
505
505
|
def to_n
|
506
506
|
`self.valueOf()`
|
507
507
|
end
|
508
508
|
end
|
509
509
|
|
510
510
|
class Time
|
511
|
-
#
|
511
|
+
# @return itself (an instance of `Date`).
|
512
512
|
def to_n
|
513
513
|
self
|
514
514
|
end
|
515
515
|
end
|
516
516
|
|
517
517
|
class NilClass
|
518
|
-
#
|
518
|
+
# @return the corresponding JavaScript value (`null`).
|
519
519
|
def to_n
|
520
520
|
`null`
|
521
521
|
end
|
@@ -526,7 +526,7 @@ class Hash
|
|
526
526
|
|
527
527
|
def initialize(defaults = undefined, &block)
|
528
528
|
%x{
|
529
|
-
if (defaults
|
529
|
+
if (defaults !== undefined && defaults.constructor === Object) {
|
530
530
|
var smap = self.$$smap,
|
531
531
|
keys = self.$$keys,
|
532
532
|
key, value;
|
@@ -559,7 +559,7 @@ class Hash
|
|
559
559
|
}
|
560
560
|
end
|
561
561
|
|
562
|
-
#
|
562
|
+
# @return a JavaScript object with the same keys but calling #to_n on
|
563
563
|
# all values.
|
564
564
|
def to_n
|
565
565
|
%x{
|
@@ -578,7 +578,7 @@ class Hash
|
|
578
578
|
value = key.value;
|
579
579
|
}
|
580
580
|
|
581
|
-
result[key] = #{Native.try_convert(`value`)};
|
581
|
+
result[key] = #{Native.try_convert(`value`, `value`)};
|
582
582
|
}
|
583
583
|
|
584
584
|
return result;
|
@@ -587,7 +587,7 @@ class Hash
|
|
587
587
|
end
|
588
588
|
|
589
589
|
class Module
|
590
|
-
#
|
590
|
+
# Exposes the current module as a property of
|
591
591
|
# the global object (e.g. `window`).
|
592
592
|
def native_module
|
593
593
|
`Opal.global[#{self.name}] = #{self}`
|
@@ -611,5 +611,5 @@ class Class
|
|
611
611
|
end
|
612
612
|
end
|
613
613
|
|
614
|
-
#
|
614
|
+
# Exposes the global value (would be `window` inside a browser)
|
615
615
|
$$ = $global = Native(`Opal.global`)
|