Skip to content

Commit

Permalink
add AudioSnippetTransform
Browse files Browse the repository at this point in the history
  • Loading branch information
pascal-giguere committed Oct 24, 2024
1 parent 629fa94 commit 95c9bd3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mastofeed",
"version": "1.5.11",
"version": "1.6.0",
"description": "A Node.js / TypeScript library to post RSS feed items to Mastodon. Outputs rich, highly-customizable posts.",
"author": "Pascal Giguère",
"homepage": "https://github.com/pascal-giguere/mastofeed#readme",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export {
UppercaseTransform,
LowercaseTransform,
CapitalizeTransform,
AudioSnippetTransform,
MapTransform,
} from './utils/transforms';
export { LogLevel } from './utils/logging';
29 changes: 29 additions & 0 deletions src/utils/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,35 @@ export class CapitalizeTransform extends Transform {
};
}

export class AudioSnippetTransform extends Transform {
private readonly label: string;

constructor(label: string) {
super();
this.label = label;
}

override apply = (value: string): string => {
let durationSeconds: number | undefined;
try {
durationSeconds = parseFloat(value);
} catch (err) {
console.error(`Error parsing audio snippet duration '${value}'. Please provide a value in seconds.`, err);
}

if (typeof durationSeconds !== 'number' || isNaN(durationSeconds)) {
console.error(`Invalid audio duration '${value}'.`);
return '';
}

const durationMinutes: number = Math.floor(durationSeconds / 60);
const durationRemainingSeconds: number = Math.round(durationSeconds % 60);
const durationString: string = `${durationMinutes} min ${durationRemainingSeconds.toString().padStart(2, '0')} sec`;

return `${this.label} (${durationString})`;
};
}

export class MapTransform extends Transform {
private readonly valueMap: Record<string, string>;
private readonly strict: boolean;
Expand Down

0 comments on commit 95c9bd3

Please sign in to comment.