Skip to content
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

refactor: upgrade deps, improve naming and perform micro optimizations #80

Merged
merged 3 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
v0.21.0

- Dependencies updated.
- Naming improved.
- Micro optimizations performed.

v0.20.0

- Code refactor
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "muninn",
"version": "0.20.0",
"version": "0.21.0",
"description": "It parses the html and collects the requested data as desired.",
"main": "build/index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions src/parser/getArrayValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function getArrayValue<Initial = unknown>(
}

$(elems).each((index, el) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { selector, type, ...rest } = config;
const value = getValue(
{ $, el: $(el) },
Expand Down
18 changes: 8 additions & 10 deletions src/parser/getSchemaValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ function getSchemaValue<Initial = unknown>(
{ $, el }: ElementPassArg,
config: Schema<Initial>
): Record<keyof Exclude<typeof config, SchemaGenerator>['schema'], unknown> {
const value = Object.keys(config).reduce((values, key) => {
return Object.keys(config).reduce((values, key) => {
const conf = config[key];
const rawConf = getRawConfig(conf);
const rawConfig = getRawConfig(conf);

if (Array.isArray(rawConf)) {
for (const rconf of rawConf) {
if (Array.isArray(rawConfig)) {
for (const rconf of rawConfig) {
const currentRawConfig = getConfig({ $, el }, rconf);

const val = getValue({ $, el }, currentRawConfig);
const value = getValue({ $, el }, currentRawConfig);

if (val !== null && val !== undefined) {
values[key] = val;
if (value !== null && value !== undefined) {
values[key] = value;
break;
}
}
Expand All @@ -31,14 +31,12 @@ function getSchemaValue<Initial = unknown>(
return values;
}

const currentRawConfig = getConfig({ $, el }, rawConf);
const currentRawConfig = getConfig({ $, el }, rawConfig);

values[key] = getValue({ $, el }, currentRawConfig);

return values;
}, {});

return value;
}

export default getSchemaValue;
22 changes: 10 additions & 12 deletions src/parser/getSimpleValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,17 @@ function getSimpleValue<Initial = unknown>(

if (html) {
value = element.html();
} else if (attr) {
if (Array.isArray(attr)) {
value = attr.reduce((acc, arg) => {
acc[arg] = element.attr(arg);
return acc;
}, {});
} else if (attr === '$all') {
value = element.attr();
} else {
value = element.attr(attr);
}
} else {
} else if (!attr) {
value = element.text();
} else if (Array.isArray(attr)) {
value = attr.reduce((acc, arg) => {
acc[arg] = element.attr(arg);
return acc;
}, {});
} else if (attr === '$all') {
value = element.attr();
} else {
value = element.attr(attr);
}

if (initial && !value) {
Expand Down
12 changes: 5 additions & 7 deletions src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,19 @@ function parse<Initial = unknown>(
$ = data;
}

const rawConf = getRawConfig(config);
const rawConfig = getRawConfig(config);

if (Array.isArray(rawConf)) {
for (const conf of rawConf) {
if (Array.isArray(rawConfig)) {
for (const conf of rawConfig) {
const value = getValue({ $, el: $ }, conf);

if (value !== null) {
return value;
}
if (value !== null) return value;
}

return null;
}

return getValue({ $ }, rawConf);
return getValue({ $ }, rawConfig);
}

export default parse;
4 changes: 2 additions & 2 deletions src/parser/transformValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ function transformValue<Initial = unknown>(
const { trim, regex, type, methods = [], transform } = config;

if (typeof value === 'string' && trim !== false) {
value = value.trim();

if (regex) {
value = execRegex(value, regex);
}

if (value) value = value.trim();
}

if (type) {
Expand Down