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

Trim tags and flavor inputs #143

Merged
merged 1 commit into from Oct 23, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions __tests__/flavor.test.ts
Expand Up @@ -152,6 +152,19 @@ describe('transform', () => {
} as Flavor,
false
],
[
[
`prefix= `,
],
{
latest: "auto",
prefix: "",
prefixLatest: false,
suffix: "",
suffixLatest: false,
} as Flavor,
false
]
])('given %p attributes ', async (inputs: string[], expected: Flavor, invalid: boolean) => {
try {
const flavor = Transform(inputs);
Expand Down
34 changes: 21 additions & 13 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 14 additions & 9 deletions src/flavor.ts
Expand Up @@ -25,39 +25,44 @@ export function Transform(inputs: string[]): Flavor {
})[0];
let onlatestfor = '';
for (const field of fields) {
const parts = field.toString().split('=', 2);
const parts = field
.toString()
.split('=')
.map(item => item.trim());
if (parts.length == 1) {
throw new Error(`Invalid flavor entry: ${input}`);
}
switch (parts[0]) {
const key = parts[0].toLowerCase();
const value = parts[1];
switch (key) {
case 'latest': {
flavor.latest = parts[1];
flavor.latest = value;
if (!['auto', 'true', 'false'].includes(flavor.latest)) {
throw new Error(`Invalid latest flavor entry: ${input}`);
}
break;
}
case 'prefix': {
flavor.prefix = parts[1];
flavor.prefix = value;
onlatestfor = 'prefix';
break;
}
case 'suffix': {
flavor.suffix = parts[1];
flavor.suffix = value;
onlatestfor = 'suffix';
break;
}
case 'onlatest': {
if (!['true', 'false'].includes(parts[1])) {
throw new Error(`Invalid value for onlatest attribute: ${parts[1]}`);
if (!['true', 'false'].includes(value)) {
throw new Error(`Invalid value for onlatest attribute: ${value}`);
}
switch (onlatestfor) {
case 'prefix': {
flavor.prefixLatest = /true/i.test(parts[1]);
flavor.prefixLatest = /true/i.test(value);
break;
}
case 'suffix': {
flavor.suffixLatest = /true/i.test(parts[1]);
flavor.suffixLatest = /true/i.test(value);
break;
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/tag.ts
Expand Up @@ -93,12 +93,15 @@ export function Parse(s: string): Tag {

const tag = new Tag();
for (const field of fields) {
const parts = field.toString().split('=', 2);
const parts = field
.toString()
.split('=')
.map(item => item.trim());
if (parts.length == 1) {
tag.attrs['value'] = parts[0].trim();
tag.attrs['value'] = parts[0];
} else {
const key = parts[0].trim().toLowerCase();
const value = parts[1].trim();
const key = parts[0].toLowerCase();
const value = parts[1];
switch (key) {
case 'type': {
if (!Object.values(Type).includes(value)) {
Expand Down