Skip to content

add sorting for results output in expand(), expandAll() and expandN(), #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
laminesadiki opened this issue Nov 8, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@laminesadiki
Copy link

laminesadiki commented Nov 8, 2022

Hey Drew , I hope that you're doing well.
Thank you for your incredible working. I use your library for a small time ago, I found it really usefull and helpful.

Otherwise, for a use case, I need to generate all the possible combinations of letters and numbers. and I see that the results is random and it is not sorted. I need to sort them by my own and that cost memory and time to do this with javascript native sorting function.

For example

const regex = '[a-e][0-5]';
const expander = expand(regex);
console.log(expander);
// output => Expansion { getIterator: [Function: beginIterating], count: 30 }

in this small example I have just 30 examples ,
If I use expandN , it will give me a randomize order

const regex = '[a-e][0-5]';
const expander = expandN(regex,5);
/*
first run    => [ 'b4', 'e3', 'a2', 'b3', 'b1' ]
second run => [ 'b5', 'c3', 'c0', 'b3', 'b0' ]
 */

I tried to use a workaround method using sort() and slice() like this

const regex = '[a-e][0-5]';
const expander = expandAll(regex).sort().slice(0, 5);
console.log(expander);
// output => [ 'a0', 'a1', 'a2', 'a3', 'a4' ]

But this is not practical, imagine I have millions of results, It will cost time and ressource to do this, and causing a memory leak in javascript.

It is better to add sorting for expand, expandAll() and expandN().
I don't know if already exists, please to add this feature.
otherwise, You can give me a clue how I can add it in the library and contribute with you.

My Best.

@wimpyprogrammer wimpyprogrammer added the enhancement New feature or request label Nov 9, 2022
@wimpyprogrammer
Copy link
Owner

Hi Ayoub,

Thanks for your feedback! I'm so glad to hear this library has been helpful.

I like your suggestion for supporting output in a non-random order. This was actually the original behavior but I didn't think it was useful, so I implemented the randomness. Hearing that an option for non-random order would be valuable to someone is helpful for prioritizing my effort.

Conceptually I've already solved this problem in the unit tests, where I stub the randomization logic to make the tests deterministic.

beforeEach(() => {
// Keep a stable order for consistent tests.
sortFn = jest
.spyOn(randomSort, 'default')
.mockImplementation((items) => [...items]);
randomWeightedFn = jest
.spyOn(chooseRandomWeighted, 'default')
.mockImplementation(() => 0);
randomFn = jest
.spyOn(chooseRandom, 'default')
.mockImplementation((minValue) => minValue);
});

I'll need to think about how best to expose this behavior. My first thought is a sub-directory like

// These functions output in random order.
import { expand, expandN, expandAll } from 'regex-to-strings';
// These functions output in sorted order.
import { expand, expandN, expandAll } from 'regex-to-strings/sorted';

similar to how Node.js has fs and fs/promise APIs that mirror each other.

If there's a different API that you think would be more intuitive, please let me know! Otherwise I will try to work on this over the weekend.

@laminesadiki
Copy link
Author

Hi Drew.

Thanks for your answer. I found the sub-directory idea is good. but it can be more perfect to add optional argument for sorting or filtering results.
It can be an objet argument or even a callback function to do a specific use case.

something like this :

import { expand, expandN, expandAll } from 'regex-to-strings';
const regex = '[a-e][0-5]';
const expander = expand(regex,{sort: "asc"}); // asc or desc

or if I want to do some filtering, like this

import { expand, expandN, expandAll } from 'regex-to-strings';
const regex = '[a-e][0-5]';
const expander = expand(regex,(str) => str.endsWith("am") ); // or any other filtreing critea.

Looking forward to see and review your next commits.
My Best.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants