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

Add ability to style suffix #14

Merged
merged 1 commit into from
Mar 4, 2024
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
21 changes: 17 additions & 4 deletions MMM-CTA.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Module.register('MMM-CTA', {
maxResultsBus: 5,
maxResultsTrain: 5,
routeIcons: true,
suffixStyle: 'long',
stops: [],
},

Expand Down Expand Up @@ -109,10 +110,22 @@ Module.register('MMM-CTA', {
if (minutesInt === 0) {
return 'DUE';
}
if (minutesInt === 1) {
return `${minutesInt.toString()} min`;
}
return this.minutesWithSuffix(minutesInt);
},

return `${minutesInt.toString()} mins`;
minutesWithSuffix(minutes) {
switch (this.config.suffixStyle) {
case 'none':
return minutes.toString();
case 'short':
return `${minutes.toString()}m`;
case 'long':
default:
if (minutes === 1) {
return `${minutes.toString()} min`;
}

return `${minutes.toString()} mins`;
}
},
});
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,20 @@ var config = {

## Configuration options

| Option | Required? | Description |
| ----------------- | ------------ | ---------------------------------------------------------------------- |
| `busApiKey` | **Required** | See [Obtaining CTA API keys](#obtaining-cta-api-keys) |
| `trainApiKey` | **Required** | See [Obtaining CTA API keys](#obtaining-cta-api-keys) |
| `stops` | **Required** | Array of stops to display. See [`stops` option](#stops-option) |
| `updateInterval` | *Optional* | Refresh time in milliseconds <br>Default 60000 milliseconds (1 minute) |
| `maxResultsBus` | *Optional* | Maximum number of bus results to display <br>Default `5` |
| `maxResultsTrain` | *Optional* | Maximum number of train results to display <br>Default `5` |
| `routeIcons` | *Optional* | True/False - Display icons next to routes. <br>Default `true` |
| Option | Required? | Description |
| ----------------- | ------------ | ----------------------------------------------------------------------------------- |
| `busApiKey` | **Required** | See [Obtaining CTA API keys](#obtaining-cta-api-keys) |
| `trainApiKey` | **Required** | See [Obtaining CTA API keys](#obtaining-cta-api-keys) |
| `stops` | **Required** | Array of stops to display. See [`stops` option](#stops-option) |
| `updateInterval` | *Optional* | Refresh time in milliseconds <br>Default 60000 milliseconds (1 minute) |
| `maxResultsBus` | *Optional* | Maximum number of bus results to display <br>Default `5` |
| `maxResultsTrain` | *Optional* | Maximum number of train results to display <br>Default `5` |
| `routeIcons` | *Optional* | True/False - Display icons next to routes. <br>Default `true` |
| `suffixStyle` | *Optional* | Style of suffix for the arrival time. `long`, `short`, or `none` <br>Default `long` |

### `stops` option

The `stops` option is an array of objects. Each object represents a stop to display.
The `stops` option is an array of objects. Each object represents a stop to display.

```js
{
Expand Down
41 changes: 41 additions & 0 deletions __tests__/MMM-CTA.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ it('has a default config', () => {
maxResultsTrain: 5,
maxResultsBus: 5,
routeIcons: true,
suffixStyle: 'long',
stops: [],
});
});
Expand Down Expand Up @@ -281,3 +282,43 @@ describe('socketNotificationReceived', () => {
});
});
});

describe('minutesWithSuffix', () => {
describe('suffix style is long', () => {
beforeEach(() => {
MMMCTA.setConfig({ suffixStyle: 'long' });
});

it('returns min for singular', () => {
expect(MMMCTA.minutesWithSuffix(1)).toBe('1 min');
});

it('returns mins for plural', () => {
expect(MMMCTA.minutesWithSuffix(2)).toBe('2 mins');
});
});

describe('suffix style is short', () => {
beforeEach(() => {
MMMCTA.setConfig({ suffixStyle: 'short' });
});

it('returns m for singular', () => {
expect(MMMCTA.minutesWithSuffix(1)).toBe('1m');
});

it('returns m for plural', () => {
expect(MMMCTA.minutesWithSuffix(2)).toBe('2m');
});
});

describe('suffix style is none', () => {
beforeEach(() => {
MMMCTA.setConfig({ suffixStyle: 'none' });
});

it('returns number', () => {
expect(MMMCTA.minutesWithSuffix(1)).toBe('1');
});
});
});
Loading