Skip to content

Commit

Permalink
feat(#9): parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Oct 2, 2024
1 parent 241219c commit b5bee45
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 12 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ repositories those were created in the provided date range.
| Option | Required | Description |
|---------------|----------|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|
| `--query` || [GitHub Search API query] |
| `--graphql` || Path to GitHub API GraphQL query, default is `ghminer.graphql` |
| `--start` || The start date to search the repositories, in [ISO] format; e.g. `2024-01-01` |
| `--end` || The end date to search the repositories, in [ISO] format; e.g. `2024-01-01` |
| `--graphql` || Path to GitHub API GraphQL query, default is `ghminer.graphql`. |
| `--schema` || Path to parsing schema, default is `ghminer.json`. |
| `--start` || The start date to search the repositories, in [ISO] format; e.g. `2024-01-01`. |
| `--end` || The end date to search the repositories, in [ISO] format; e.g. `2024-01-01`. |
| `--tokens` || Text file name that contains a number of [GitHub PATs]. Those will be used in order to pass GitHub API rate limits. Add as many tokens as needed, considering the amount of data (they should be separated by line break). |
| `--date` || The type of the date field to search on, you can choose from `created`, `updated` and `pushed`, the default one is `created`. |
| `--batchsize` || Request batch-size value in the range `10..100`. The default value is `10`. |
Expand Down
44 changes: 35 additions & 9 deletions src/nested-prop.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 Aliaksei Bialiauski
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

const nestedProp = (obj, path) => {
return path.split('.').reduce((acc, key) => {
if (!acc) return null;
const match = key.match(/^([a-zA-Z_$][a-zA-Z_$0-9]*)\[(\d+)\]$/);
const match = key.match(/^([a-zA-Z_$][a-zA-Z_$0-9]*)\[(\d*)\]$/);
if (match) {
const arrayKey = match[1]; // The part before [0]
const index = parseInt(match[2], 10); // The index number inside []
return acc[arrayKey] && Array.isArray(acc[arrayKey]) ? acc[arrayKey][index] : null;
}
if (key.endsWith('[]')) {
const arrayKey = key.slice(0, -2);
return acc[arrayKey] && Array.isArray(acc[arrayKey]) ? acc[arrayKey].map(item => item.node) : [];
const arrayKey = match[1];
const index = match[2];
if (index) {
return acc[arrayKey] && Array.isArray(acc[arrayKey]) ? acc[arrayKey][parseInt(index, 10)] : null;
} else {
return acc[arrayKey] && Array.isArray(acc[arrayKey]) ? acc[arrayKey] : [];
}
}
return acc[key] !== undefined ? acc[key] : null;
return Array.isArray(acc)
? acc.map(item => item[key] !== undefined ? item[key] : null)
: acc[key] !== undefined ? acc[key] : null;
}, obj);
};

Expand Down
39 changes: 39 additions & 0 deletions test/nested-prop.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,43 @@ describe('Test case for nested-prop.js', function () {
`parsed ${prop} does not match with expected ${expected}`
);
});
it('returns array of nested props', function () {
const obj = {
repositoryTopics: {
edges: [
{
node: {
topic: {
name: "java"
}
}
},
{
node: {
topic: {
name: "jvm"
}
}
},
{
node: {
topic: {
name: "compilers"
}
}
}
]
}
};
const prop = nested(
obj,
"repositoryTopics.edges[].node.topic.name"
);
const expected = 'java,jvm,compilers';
assert.equal(
prop,
expected,
`parsed ${prop} does not match with expected ${expected}`
);
});
});

0 comments on commit b5bee45

Please sign in to comment.