Skip to content

Instantly share code, notes, and snippets.

@dekrain
Created November 30, 2016 19:48
Show Gist options
  • Save dekrain/c6057954e04ad1485ff71b1ef7f4f48e to your computer and use it in GitHub Desktop.
Save dekrain/c6057954e04ad1485ff71b1ef7f4f48e to your computer and use it in GitHub Desktop.
Snap! query!
/** @fileoverview
* This file ISN'T part of Snap!. It's only plug-in made by ~DeKrain
*/
var exts = ['#'];
function $s(query,
wrld /* optional */
) {
var ext = query[0], world = wrld || window.world, result, allchilds;
if (exts.indexOf(ext) === -1)
ext = '[default]';
if (ext !== '[default]')
query = query.slice(1);
allchilds = world.allChildren();
switch (ext) {
case '#':
result = allchilds.filter(function(child){
return child.name === query ||
child.key === query || // Dialog key
child.selector === query; // Blocks
}); break;
default: // Class
result = allchilds.filter(function(child){return child.constructor.name === query; });
}
return (result.length > 1 ) ? result : (result.length !== 0) ? result[0] : null;
}

This file ISN'T part of Snap!. It's only plug-in made by ~DeKrain

Types of query:

exts = ['#']

Query function:

window.$s = (

query string

	query,

optional world

	wrld
) ->
	ext = query[0]
	world = wrld or window.world
	result = undefined

	ext = '[default]' if exts.indexOf(ext) is -1
	query = query[1..] if ext isnt '[default]'
	allchilds = world.allChildren()
	switch ext
		when '#'
			result = allchilds.filter (child) ->

sprite name, etc.

				child.name is query or

dialog boxes

				child.key is query or

blocks

				child.selector is query

Class:

		else
			result = allchilds.filter (child) -> child.constructor.name is query
	if result.length > 1 then result else (if result.length isnt 0 then result[0] else null)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment