Skip to content

Commit

Permalink
Merge pull request #14 from JHWelch/add-suffix-options
Browse files Browse the repository at this point in the history
Add ability to style suffix
  • Loading branch information
JHWelch authored Mar 4, 2024
2 parents a9079fa + 387f9a1 commit d2be0db
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 14 deletions.
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');
});
});
});

0 comments on commit d2be0db

Please sign in to comment.