Skip to content

GeoArchive/json-javascript-fast-search

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 

Repository files navigation

json javascript : fast Search

You have a json object that is an array of objects containing a primary key? Find things FAST with this function, it's just under 200 lines (without the comment lines) and uncompressed code!

This function was developed for the GeoArchive project.
Take a look: general info Geoarchive >> / image library 'in action' >>

Json objects can becomde big. In our case easily over 10Mb. Creating a simple "for loop" to match what was needed to be found could take up to several minutes. Then mrAceT had a great idea.. splitting things up via the native JavaScript split function.. and after some extra nifty regular expressions.. the created search function gives results in a split second! (litteraly;)

The settings

The basics are quite self explanetory:

  let defaults = {
    delimiter    : '"',     // the character that is the delimiter of the key / value
    id           : 'id',    // primary key
    search       : '',      // search string / 'regex' => [*] = special wildcard : negativeIdPattern
    searchSpecial: 'auto',  // match special characters, like: match an a-acute char to an a and á auto = not for regex (gets messy fast..)
    seachIgnore  : ['id'],  // exclude key values from positive search result for nothing set to []
    idPos        : 'auto',  // auto | before | after (where is the id relative to the search)
    //                         Only when set to 'auto' there will be validation
    idPosLast    : 'before',// before | after (last result, default expectation: before )
    return       : 'first', // first | firstpos | object | array
    Str          : '',      // stringified json data
    Obj          : null,    // json object
    debug        : 0,       // set to > 0 for debugging
  };

you can choose serveral different results to be returned:
first: get the first complete object that contains the search result
firstpos: get the index position of the first object that contains the search result
object: get all the results in an object matching the search
array: get all the id's in an array matching the search

The exiting part is the search variable!
simply a string?: simply put it in there
Want a wildcard? use the character * like: this*that (the * will be replaced by a nifty regex excluding the id field, so that a match will be only be made within one record
Want the result to start with your search string? use the ^ like: ^word (the ^ will be replaced by the 'delimiter')
Want the result to end with your search string? use the $ like: word$ (the $ will be replaced by the 'delimiter')

regular expression?: are the extra options not enough, you can unleash the full power of regular expressions!
There is one important note: do not use .* to catch "anything". That could bleed into multiple id's! use: [*] in stead!

Examples:

Lets assume you have an object that could contain the key image (using the default settings shown above):

jsonData = [
  {"id":"id1",                      "description":"only id & description"},
  {"id":"id2","image":""           ,"description":"empty key 'image' will also match"},
  {"id":"id3","image":"picture.jpg","description":"nice image"},
  {"id":"id4","image":"picture.png","description":"nice image uncompressed"},
]

You want to get the first object that contains the word image:
let result = jsonFastSearch(jsonData, 'image' );
The first variable in the function call can be a stringified json string, or a json object (there is no speed difference, both are needed, so the other must be created).
When the second variable in the function call is a string, that will become the search command, otherwise it needs to be an object with what you want different then your set defaults.

You want to get the first key of an object that contains the word image:
let result = jsonFastSearch(jsonData, { search: 'image', return : 'firstpos' } );
Now you have the position, not the key, so to get the key: result = jsonData[result].id;

You want to get all the keys that have an image value:
let result = jsonFastSearch(jsonData, { search: '/"image"\s*:\s*"[^"]/', return: 'array' } );
Where " is the same as your delimiter

You want to get all the objects that have an image value:
let result = jsonFastSearch(jsonData, { search: '/"image"\s*:\s*"[^"]/', return: 'object' } );
Where " is the same as your delimiter

many searches on same json?:
You can parse a json or a stringified json to the function via the first variable. If you perform many searches on the same json you can parse 'the other one' via the options to save the time of the conversion at each search, example:
let result = jsonFastSearch(jsonData, { search: '/"image"\s*:\s*"[^"]/', return: 'object', Str:JSON.stringify(jsonData) } );
Or if you your basis is the stringified json: let result = jsonFastSearch(jsonStr, { search: '/"image"\s*:\s*"[^"]/', return: 'object', Obj:JSON.parse(jsonStr) } );\

version history

v1.00.010 added option to parse "the other one" for faster many repeated searches (Str & Obj)
v1.00.009 changed versioning, default idPosLast:before is better, improved debugging
v1.00.008 fixed bugs and improved regex search and added option seachIgnore
v1.00.007 fixed bugs and improved search
v1.00.006 first commit

I give to you, you..

The GeoArchive project contains quite a bit of FOSS software, so I think it's only fair to give something back every now and then and you might have the need fot this tool also, so here it is!

Like the work? You'll be surprised how much time goes into things like this..
Be my hero, think about the time this script saved you, and (offcourse) how happy you are now that you found it and it works just like you wanted. Support my work, buy me a cup of coffee, give what its worth to you, or give me half the time this script saved you ;)

Contributing

If you find this script useful and you make some modifications please make a pull request so that others can benefit from it. This would be highly appreciated!

License

This script is open-sourced software licensed under the GNU GENERAL PUBLIC LICENSE. Please see LICENSE for details.

About

Have a json object with a primary key? Find things FAST with this function!

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published