npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rbxts/roselect

v0.1.2

Published

Simple “selector” library for Rodux

Downloads

387

Readme


License MIT Package

Usability

  • Selectors can compute derived data, allowing state managers to store the minimal possible state.
  • Selectors are efficient. A selector is not recomputed unless one of its arguments changes.
  • Selectors are composable. They can be used as input to other selectors.
local roselect = require(path.to.roselect)
local createSelector = roselect.createSelector
local reduce = roselect.reduce

local function shopItemsSelector(state)
        return state.shop.items
end

local function taxPercentSelector(state)
        return state.shop.taxPercent
end

local subtotalSelector = createSelector(
	shopItemsSelector,
	function(items)
                return reduce(items, function(acc, item)
                        return acc + item.value
                end, 0)
        end
)

local taxSelector = createSelector(
        subtotalSelector,
        taxPercentSelector,
        function(subtotal, taxPercent)
                return subtotal * (taxPercent / 100)
        end
)

local totalSelector = createSelector(
        subtotalSelector,
	taxSelector,
        function(subtotal, tax)
                return { total = subtotal + tax }
        end
)

local exampleState = {
        shop = {
                taxPercent = 8,
                items = {
			{ name = "apple", value = 1.20 },
			{ name = "orange", value = 0.95 }
                }
        }
}

print(subtotalSelector(exampleState)) -- 2.15
print(taxSelector(exampleState))      -- 0.172
print(totalSelector(exampleState))    -- { total = 2.322 }
import { createSelector } from "@rbxts/roselect";

interface State {
	shop: {
		taxPercent: number;
		items: { name: string; value: number; }[];
	}
}

const shopItemsSelector = (state: State) => state.shop.items;
const taxPercentSelector = (state: State) => state.shop.taxPercent;

const subtotalSelector = createSelector(
	shopItemsSelector,
	items => items.reduce((acc, item) => acc + item.value, 0)
);

const taxSelector = createSelector(
	subtotalSelector,
	taxPercentSelector,
	(subtotal, taxPercent) => subtotal * (taxPercent / 100)
);

export const totalSelector = createSelector(
	subtotalSelector,
	taxSelector,
	(subtotal, tax) => ({ total: subtotal + tax })
);

const exampleState = identity<State>({
	shop: {
		taxPercent: 8,
		items: [
			{ name: 'apple', value: 1.20 },
			{ name: 'orange', value: 0.95 },
		]
	}
});

print(subtotalSelector(exampleState)); // 2.15
print(taxSelector(exampleState));      // 0.172
print(totalSelector(exampleState));    // { total: 2.322 }

Installation

luau

git clone https://github.com/HylianBasement/roselect.git ./modules

roblox-ts

The installation can be done via npm i @rbxts/roselect.

Composing Selectors

It's important to mention that createSelector checks if the first parameter is an array of dependencies. Since lua tables are both dictionary and list, it treats that parameter as an array of selectors that needs to be composed. A memoized selector can itself be an input-selector to another memoized selector. Here is getVisibleTodos being used as an input-selector to a selector that further filters the todos by keyword:

local function getVisibilityFilter(state)
	return state.visibilityFilter
end

local function getTodos(state)
	return state.todos
end

local function getKeyword(state)
	return state.keyword
end

local getVisibleTodos = createSelector(
	{ getVisibilityFilter, getTodos },
	function(visibilityFilter, todos)
		if visibilityFilter == "SHOW_ALL" then
			return todos
		elseif visibilityFilter == "SHOW_COMPLETED" then
			local newTodos = {}

			for k, todo in pairs(todos) do
				if todo.completed then
					newTodos[k] = todo
				end
			end

			return newTodos
		elseif visibilityFilter == "SHOW_ACTIVE" then
			local newTodos = {}

			for k, todo in pairs(todos) do
				if not todo.completed then
					newTodos[k] = todo
				end
			end

			return newTodos
		end
	end
)

local getVisibleTodosFilteredByKeyword = createSelector(
	{ getVisibleTodos, getKeyword },
  	function(visibleTodos, keyword)
		local newTodos = {}

		for k, todo in pairs(visibleTodos) do
			if todo.text:find(keyword) then
				newTodos[k] = todo
			end
		end

		return newTodos
  	end
)

return {
	getVisibleTodos = getVisibleTodos
}
const getVisibilityFilter = (state: State) => state.visibilityFilter;
const getTodos = (state: State) => state.todos;
const getKeyword = (state: State) => state.keyword;
 
export const getVisibleTodos = createSelector(
	[ getVisibilityFilter, getTodos ],
	(visibilityFilter, todos) => {
		switch (visibilityFilter) {
		case "SHOW_ALL":
			return todos;
		case "SHOW_COMPLETED":
			return todos.filter(t => t.completed);
		case "SHOW_ACTIVE":
			return todos.filter(t => !t.completed);
		}
	}
);

const getVisibleTodosFilteredByKeyword = createSelector(
	[ getVisibleTodos, getKeyword ],
	(visibleTodos, keyword) => visibleTodos.filter(
		todo => todo.text.includes(keyword)
	)
);

Can Roselect be used without Rodux?

Definitely. Just like the JS library, Roselect has no dependencies on any other package, so it can be used independently.

License

This project is licensed under the MIT License.