@Orimay put validation: adding '-Infinity' as an invalid case; speeding validation up (#1189)

* Adding '-Infinity' as an invalid case

Adding '-Infinity' as an invalid case, not handled before
Skipping extra checks until needed (like NaN, Infinity)
Checking for 'null' first as for more common case than invalid 'undefined'

* Adding '-Infinity' as an invalid case

Adding '-Infinity' as an invalid case, not handled before
Skipping extra checks until needed (like NaN, Infinity)
Checking for 'null' first as for more common case than invalid 'undefined'

* Adding '-Infinity' as an invalid case

Adding '-Infinity' as an invalid case, not handled before
Skipping extra checks until needed (like NaN, Infinity)
Checking for 'null' first as for more common case than invalid 'undefined'

* Fixing tests to handle -Infinity

* Removing useless shim import

* Removing useless shim import

* Removing redundant undefined check

* Removing redundant undefined check

* Removing redundant undefined check

* ES5 compatibility

Co-authored-by: dbaranov <dbaranov@bellintegrator.com>
Co-authored-by: Mark Nadal <mark@gun.eco>
This commit is contained in:
Orimay 2022-02-04 13:41:22 +03:00 committed by GitHub
parent f85f55c5a1
commit 05f497778a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 41 deletions

26
gun.js
View File

@ -130,19 +130,19 @@
})(USE, './onto'); })(USE, './onto');
;USE(function(module){ ;USE(function(module){
USE('./shim'); // Valid values are a subset of JSON: null, binary, number (!Infinity), text,
module.exports = function(v){ // Valid values are a subset of JSON: null, binary, number (!Infinity), text, or a soul relation. Arrays need special algorithms to handle concurrency, so they are not supported directly. Use an extension that supports them if needed but research their problems first. // or a soul relation. Arrays need special algorithms to handle concurrency,
if(v === undefined){ return false } // so they are not supported directly. Use an extension that supports them if
if(v === null){ return true } // "deletes", nulling out keys. // needed but research their problems first.
if(v === Infinity){ return false } // we want this to be, but JSON does not support it, sad face. module.exports = function (v) {
if(v !== v){ return false } // can you guess what this checks for? ;) // "deletes", nulling out keys.
if('string' == typeof v // text! return v === null ||
|| 'boolean' == typeof v "string" === typeof v ||
|| 'number' == typeof v){ "boolean" === typeof v ||
return true; // simple values are valid. // we want +/- Infinity to be, but JSON does not support it, sad face.
} // can you guess what v === v checks for? ;)
if(v && ('string' == typeof (v['#']||0)) && Object.empty(v, ['#'])){ return v['#'] } // is link ("number" === typeof v && v != Infinity && v != -Infinity && v === v) ||
return false; // If not, everything else remaining is an invalid data type. Custom extensions can be built on top of these primitives to support other types. (v && "string" == typeof v["#"] && Object.keys(v).length === 1 && v["#"]);
} }
})(USE, './valid'); })(USE, './valid');

View File

@ -156,17 +156,21 @@
var obj = Type.obj, obj_is = obj.is, obj_has = obj.has, obj_map = obj.map; var obj = Type.obj, obj_is = obj.is, obj_has = obj.has, obj_map = obj.map;
var Val = {}; var Val = {};
Val.is = function(v){ DEP('val.is'); // Valid values are a subset of JSON: null, binary, number (!Infinity), text, or a soul relation. Arrays need special algorithms to handle concurrency, so they are not supported directly. Use an extension that supports them if needed but research their problems first. // Valid values are a subset of JSON: null, binary, number (!Infinity), text,
if(v === u){ return false } // or a soul relation. Arrays need special algorithms to handle concurrency,
if(v === null){ return true } // "deletes", nulling out keys. // so they are not supported directly. Use an extension that supports them if
if(v === Infinity){ return false } // we want this to be, but JSON does not support it, sad face. // needed but research their problems first.
if(text_is(v) // by "text" we mean strings. Val.is = function (v) {
|| bi_is(v) // by "binary" we mean boolean. DEP("val.is");
|| num_is(v)){ // by "number" we mean integers or decimals. // "deletes", nulling out keys.
return true; // simple values are valid. return v === null ||
} "string" === typeof v ||
return Val.link.is(v) || false; // is the value a soul relation? Then it is valid and return it. If not, everything else remaining is an invalid data type. Custom extensions can be built on top of these primitives to support other types. "boolean" === typeof v ||
} // we want +/- Infinity to be, but JSON does not support it, sad face.
// can you guess what v === v checks for? ;)
("number" === typeof v && v != Infinity && v != -Infinity && v === v) ||
(v && "string" == typeof v["#"] && Object.keys(v).length === 1 && v["#"]);
};
Val.link = Val.rel = {_: '#'}; Val.link = Val.rel = {_: '#'};
;(function(){ ;(function(){
Val.link.is = function(v){ DEP('val.link.is'); // this defines whether an object is a soul relation or not, they look like this: {'#': 'UUID'} Val.link.is = function(v){ DEP('val.link.is'); // this defines whether an object is a soul relation or not, they look like this: {'#': 'UUID'}

View File

@ -1,16 +1,14 @@
// Valid values are a subset of JSON: null, binary, number (!Infinity), text,
require('./shim'); // or a soul relation. Arrays need special algorithms to handle concurrency,
module.exports = function(v){ // Valid values are a subset of JSON: null, binary, number (!Infinity), text, or a soul relation. Arrays need special algorithms to handle concurrency, so they are not supported directly. Use an extension that supports them if needed but research their problems first. // so they are not supported directly. Use an extension that supports them if
if(v === undefined){ return false } // needed but research their problems first.
if(v === null){ return true } // "deletes", nulling out keys. module.exports = function (v) {
if(v === Infinity){ return false } // we want this to be, but JSON does not support it, sad face. // "deletes", nulling out keys.
if(v !== v){ return false } // can you guess what this checks for? ;) return v === null ||
if('string' == typeof v // text! "string" === typeof v ||
|| 'boolean' == typeof v "boolean" === typeof v ||
|| 'number' == typeof v){ // we want +/- Infinity to be, but JSON does not support it, sad face.
return true; // simple values are valid. // can you guess what v === v checks for? ;)
("number" === typeof v && v != Infinity && v != -Infinity && v === v) ||
(v && "string" == typeof v["#"] && Object.keys(v).length === 1 && v["#"]);
} }
if(v && ('string' == typeof (v['#']||0)) && Object.empty(v, ['#'])){ return v['#'] } // is link
return false; // If not, everything else remaining is an invalid data type. Custom extensions can be built on top of these primitives to support other types.
}

View File

@ -518,6 +518,7 @@ describe('Gun', function(){
expect(Gun.is('')).to.be(false); expect(Gun.is('')).to.be(false);
expect(Gun.is('a')).to.be(false); expect(Gun.is('a')).to.be(false);
expect(Gun.is(Infinity)).to.be(false); expect(Gun.is(Infinity)).to.be(false);
expect(Gun.is(-Infinity)).to.be(false);
expect(Gun.is(NaN)).to.be(false); expect(Gun.is(NaN)).to.be(false);
expect(Gun.is([])).to.be(false); expect(Gun.is([])).to.be(false);
expect(Gun.is([1])).to.be(false); expect(Gun.is([1])).to.be(false);
@ -535,6 +536,7 @@ describe('Gun', function(){
expect(Gun.valid({'#':'somesoulidhere'})).to.be('somesoulidhere'); expect(Gun.valid({'#':'somesoulidhere'})).to.be('somesoulidhere');
expect(Gun.valid({'#':'somesoulidhere', and: 'nope'})).to.be(false); expect(Gun.valid({'#':'somesoulidhere', and: 'nope'})).to.be(false);
expect(Gun.valid(Infinity)).to.be(false); // boohoo :( expect(Gun.valid(Infinity)).to.be(false); // boohoo :(
expect(Gun.valid(-Infinity)).to.be(false); // boohoo :(
expect(Gun.valid(NaN)).to.be(false); expect(Gun.valid(NaN)).to.be(false);
expect(Gun.valid([])).to.be(false); expect(Gun.valid([])).to.be(false);
expect(Gun.valid([1])).to.be(false); expect(Gun.valid([1])).to.be(false);
@ -554,6 +556,7 @@ describe('Gun', function(){
expect('string' == typeof Gun.valid(0)).to.be(false); expect('string' == typeof Gun.valid(0)).to.be(false);
expect('string' == typeof Gun.valid(1)).to.be(false); expect('string' == typeof Gun.valid(1)).to.be(false);
expect('string' == typeof Gun.valid(Infinity)).to.be(false); // boohoo :( expect('string' == typeof Gun.valid(Infinity)).to.be(false); // boohoo :(
expect('string' == typeof Gun.valid(-Infinity)).to.be(false); // boohoo :(
expect('string' == typeof Gun.valid(NaN)).to.be(false); expect('string' == typeof Gun.valid(NaN)).to.be(false);
expect('string' == typeof Gun.valid([])).to.be(false); expect('string' == typeof Gun.valid([])).to.be(false);
expect('string' == typeof Gun.valid([1])).to.be(false); expect('string' == typeof Gun.valid([1])).to.be(false);