Skip to content

Commit

Permalink
Add reverse option to index seed.
Browse files Browse the repository at this point in the history
Part #46.
  • Loading branch information
jkomoros committed Jul 30, 2023
1 parent 10cb766 commit 44ebab8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ If the item is not found, will return null.
Required parameters
- `container` - The string, object, or array to search within.
- `search` - The thing to find within the container.
- `reverse` - (optional) a boolean of whether to search from back to front. Defaults to false.
#### slice
Expand Down
21 changes: 21 additions & 0 deletions seed-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2445,6 +2445,27 @@
"description": "The thing to search for"
}
]
},
"reverse": {
"anyOf": [
{
"$ref": "#/definitions/seedData"
},
{
"$ref": "#/definitions/seedData/anyOf/0/properties/prompt/anyOf/1"
},
{
"anyOf": [
{
"not": {}
},
{
"type": "boolean"
}
],
"description": "Whether to search from the back to front"
}
]
}
},
"required": [
Expand Down
14 changes: 12 additions & 2 deletions src/grow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,21 +630,31 @@ const growIndex = async (seed : Seed<SeedDataIndex>, env : Environment) : Promis
if (container instanceof Embedding) container = container.text;
if (search instanceof Embedding) search = search.text;

const reverse = await getProperty(seed, env, data.reverse, false);

if (Array.isArray(container)) {
if (reverse) {
for (let i = container.length - 1; i >= 0; i--) {
if (container[i] == search) return i;
}
return null;
}
for (let i = 0; i < container.length; i++) {
if (container[i] == search) return i;
}
return null;
}
if (container && typeof container == 'object') {
for (const [key, value] of Object.entries(container)) {
const entries = Object.entries(container);
if (reverse) entries.reverse();
for (const [key, value] of entries) {
if (value == search) return key;
}
return null;
}
if (typeof container != 'string') throw new Error('container must be array, object, or string');
if (typeof search != 'string') throw new Error('If container is string search must also be string');
const result = container.indexOf(search);
const result = reverse ? container.lastIndexOf(search) : container.indexOf(search);
return result < 0 ? null : result;
};

Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,8 @@ const seedDataConfigIndex = {
type: z.literal('index'),
properties: {
container: inputValue.describe('The thing to search within'),
search: inputValue.describe('The thing to search for')
search: inputValue.describe('The thing to search for'),
reverse: z.boolean().optional().describe('Whether to search from the back to front')
}
};

Expand Down

0 comments on commit 44ebab8

Please sign in to comment.