How to use the from.Array function in from

To help you get started, we’ve selected a few from examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github fatcerberus / miniSphere / assets / system / game_modules / console.js View on Github external
function executeCommand(console, command)
{
	// tokenize the command string
	let tokens = command.match(/'.*?'|".*?"|\S+/g);
	if (tokens == null)
		return;
	for (let i = 0; i < tokens.length; ++i) {
		tokens[i] = tokens[i].replace(/'(.*)'/, "$1");
		tokens[i] = tokens[i].replace(/"(.*)"/, "$1");
	}
	let objectName = tokens[0];
	let instruction = tokens[1];

	// check that the instruction is valid
	if (!from.Array(console.commands)
		.any(it => it.entity == objectName))
	{
		console.log(`unrecognized object name '${objectName}'`);
		return;
	}
	if (tokens.length < 2) {
		console.log(`missing instruction for '${objectName}'`);
		return;
	}
	if (!from.Array(console.commands)
		.where(it => it.entity == objectName)
		.any(it => it.instruction == instruction))
	{
		console.log(`instruction '${instruction}' not valid for '${objectName}'`);
		return;
	}
github fatcerberus / miniSphere / assets / system / game_modules / joypad.js View on Github external
*  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
**/

'use strict';
const from = require('from');

// historically, Sphere requires a gamepad with at least 2 axes (X/Y) and
// 5 buttons (A, B, X, Y, Start) for full operation.
const Devices = from.Array(Joystick.getDevices())
	.where(it => it.numAxes >= 2)
	.where(it => it.numButtons >= 5)
	.toArray();

class Joypad
{
	constructor()
	{
		throw new TypeError(`'${new.target.name}' is a static class and cannot be instantiated`);
	}

	static get P1() { return Devices[0] || Joystick.Null; }
	static get P2() { return Devices[1] || Joystick.Null; }
	static get P3() { return Devices[2] || Joystick.Null; }
	static get P4() { return Devices[3] || Joystick.Null; }
}
github fatcerberus / miniSphere / assets / system / modules / console.js View on Github external
}
	var entity = tokens[0];
	var instruction = tokens[1];

	// check that the instruction is valid
	if (!from.Array(commands)
		.any(function(c) { return entity == c.entity; }))
	{
		log("unrecognized object name '" + entity + "'");
		return;
	}
	if (tokens.length < 2) {
		log("missing instruction for '" + entity + "'");
		return;
	}
	if (!from.Array(commands)
		.where(function(c) { return entity == c.entity; })
		.any(function(c) { return instruction == c.instruction; }))
	{
		log("instruction '" + instruction + "' not valid for '" + entity + "'");
		return;
	}

	// parse arguments
	for (var i = 2; i < tokens.length; ++i) {
		var maybeNumber = parseFloat(tokens[i]);
		tokens[i] = !isNaN(maybeNumber) ? maybeNumber : tokens[i];
	}

	// execute the command
	from.Array(commands)
		.where(function(c) { return entity == c.entity; })
github fatcerberus / miniSphere / assets / system / game_modules / scene.js View on Github external
function stop()
	{
		from.Array(tasks).each(function(tid) {
			Thread.kill(tid);
		});
	};
github fatcerberus / miniSphere / assets / system / runtime / dataWriter.js View on Github external
'uint8', 'uint16be', 'uint16le', 'uint32be', 'uint32le',
		'fstring', 'lstr8', 'lstr16be', 'lstr16le', 'lstr32be', 'lstr32le',
		'bool', 'raw',
	];
	const Attributes = {
		'fstring': [ 'length' ],
		'raw':     [ 'size' ],
	};

	for (const key of Object.keys(desc)) {
		let fieldDesc = desc[key];
		let fieldType = fieldDesc.type;
		if (!from.Array(FieldTypes).any(it => it === fieldType))
			throw new TypeError(`invalid field type '${fieldType}'`);
		if (fieldType in Attributes) {
			let haveAttributes = from.Array(Attributes[fieldType])
				.all(it => it in fieldDesc);
			if (!haveAttributes)
				throw new TypeError(`missing attributes for '${fieldType}'`);
		}
	}
}
github fatcerberus / miniSphere / assets / system / game_modules / thread.js View on Github external
static kill(threadID)
	{
		from.Array(threads)
			.where(it => it.id == threadID)
			.besides(it => it.isValid = false)
			.remove();
	}
github fatcerberus / miniSphere / assets / system / runtime / dataStream.js View on Github external
'uint8', 'uint16be', 'uint16le', 'uint32be', 'uint32le',
		'fstring', 'lstr8', 'lstr16be', 'lstr16le', 'lstr32be', 'lstr32le',
		'bool', 'raw',
	];
	const Attributes = {
		'fstring': [ 'length' ],
		'raw':     [ 'size' ],
	};

	for (const key of Object.keys(desc)) {
		let fieldDesc = desc[key];
		let fieldType = fieldDesc.type;
		if (!from.Array(FieldTypes).any(it => it === fieldType))
			throw new TypeError(`invalid field type '${fieldType}'`);
		if (fieldType in Attributes) {
			let haveAttributes = from.Array(Attributes[fieldType])
				.all(it => it in fieldDesc);
			if (!haveAttributes)
				throw new TypeError(`missing attributes for '${fieldType}'`);
		}
	}
}
github fatcerberus / miniSphere / assets / system / runtime / dataStream.js View on Github external
const FieldTypes = [
		'float32be', 'float32le', 'float64be', 'float64le',
		'int8', 'int16be', 'int16le', 'int32be', 'int32le',
		'uint8', 'uint16be', 'uint16le', 'uint32be', 'uint32le',
		'fstring', 'lstr8', 'lstr16be', 'lstr16le', 'lstr32be', 'lstr32le',
		'bool', 'raw',
	];
	const Attributes = {
		'fstring': [ 'length' ],
		'raw':     [ 'size' ],
	};

	for (const key of Object.keys(desc)) {
		let fieldDesc = desc[key];
		let fieldType = fieldDesc.type;
		if (!from.Array(FieldTypes).any(it => it === fieldType))
			throw new TypeError(`invalid field type '${fieldType}'`);
		if (fieldType in Attributes) {
			let haveAttributes = from.Array(Attributes[fieldType])
				.all(it => it in fieldDesc);
			if (!haveAttributes)
				throw new TypeError(`missing attributes for '${fieldType}'`);
		}
	}
}
github fatcerberus / miniSphere / assets / system / game_modules / thread.js View on Github external
function updateAllThreads()
{
	let activeThreads = from.Array(threads.slice())
		.where(it => it.isValid && !it.isBusy)
	let threadsEnding = [];
	for (let thread of activeThreads) {
		let lastSelf = currentSelf;
		thread.isBusy = true;
		currentSelf = thread.id;
		let isRunning = thread.updater(thread.id);
		if (thread.inputHandler !== undefined && isRunning)
			thread.inputHandler();
		currentSelf = lastSelf;
		thread.isBusy = false;
		if (!isRunning)
			threadsEnding.push(thread.id);
	}
	for (let threadID of threadsEnding)
		Thread.kill(threadID);
github fatcerberus / miniSphere / assets / system / runtime / dataStream.js View on Github external
'uint8', 'uint16be', 'uint16le', 'uint32be', 'uint32le',
		'fstring', 'lstr8', 'lstr16be', 'lstr16le', 'lstr32be', 'lstr32le',
		'bool', 'raw',
	];
	const Attributes = {
		'fstring': [ 'length' ],
		'raw':     [ 'size' ],
	};

	for (const key of Object.keys(desc)) {
		let fieldDesc = desc[key];
		let fieldType = fieldDesc.type;
		if (!from.Array(FieldTypes).any(it => it === fieldType))
			throw new TypeError(`invalid field type '${fieldType}'`);
		if (fieldType in Attributes) {
			let haveAttributes = from.Array(Attributes[fieldType])
				.all(it => it in fieldDesc);
			if (!haveAttributes)
				throw new TypeError(`missing attributes for '${fieldType}'`);
		}
	}
}

from

Easy way to make a Readable Stream

MIT
Latest version published 7 years ago

Package Health Score

67 / 100
Full package analysis