Skip to content
Open
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
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,23 +256,33 @@ Alias for `titleCase`
### Arrays

##### oxford
Converts a list of items to a human readable string with an optional limit.
Converts a list of items to a human readable string with an optional limit, limit string, and conjunction.

```javascript
items = ['apple', 'orange', 'banana', 'pear', 'pineapple']

Humanize.oxford(items)
// "apple, orange, banana, pear, and pineapple"

// Limit items shown
Humanize.oxford(items, 3)
// "apple, orange, banana, and 2 others"

// Pluralizes properly too!
Humanize.oxford(items, 4)
// "apple, orange, banana, pear, and 1 other"

// Specify a limit string
Humanize.oxford(items, 3, "and some other fruits")
// "apple, orange, banana, and some other fruits"

// Pick a conjunction (defaults to 'and')
Humanize.oxford(items, null, null, "or")
// "apple, orange, banana, pear, or pineapple"

// Also works with a limit
Humanize.oxford(items, 3, null, "with")
// "apple, orange, banana, with 2 others"
```

##### frequency
Expand Down Expand Up @@ -317,7 +327,7 @@ Please don't edit files in the `dist` subdirectory as they are generated through

## Compiling

`npm run install && npm run build`
`npm install && npm run build`

And that's it!

Expand Down
9 changes: 9 additions & 0 deletions __tests__/humanize.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,15 @@ describe('Converting a list to a readable, oxford commafied string', () => {
expect(Humanize.oxford(items.slice(0), 3, limitStr)).toEqual(`apple, orange, banana${limitStr}`);
expect(Humanize.oxford(items.slice(0, 3), 3, limitStr)).toEqual('apple, orange, and banana');
});

it('should accept custom conjunctions', () => {
const conjunction = 'or';
const limitStr = ', and some other fruits';

expect(Humanize.oxford(items.slice(0), null, null, conjunction)).toEqual(`apple, orange, banana, pear, ${conjunction} pineapple`);
expect(Humanize.oxford(items.slice(0), 3, limitStr, conjunction)).toEqual(`apple, orange, banana${limitStr}`);
expect(Humanize.oxford(items.slice(0), 3, null, conjunction)).toEqual(`apple, orange, banana, ${conjunction} 2 others`);
})
});

describe('Converting a hashmap to a dictionary-like string', () => {
Expand Down
8 changes: 5 additions & 3 deletions dist/humanize.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,20 +355,22 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol

// Converts a list of items to a human readable string with an optional limit.
oxford: function oxford(items, limit, limitStr) {
var conjunction = arguments.length <= 3 || arguments[3] === undefined ? 'and' : arguments[3];

var numItems = items.length;

var limitIndex = void 0;
if (numItems < 2) {
return String(items);
} else if (numItems === 2) {
return items.join(' and ');
return items.join(' ' + conjunction + ' ');
} else if (exists(limit) && numItems > limit) {
var extra = numItems - limit;
limitIndex = limit;
limitStr = exists(limitStr) ? limitStr : ', and ' + extra + ' ' + Humanize.pluralize(extra, 'other');
limitStr = exists(limitStr) ? limitStr : ', ' + conjunction + ' ' + extra + ' ' + Humanize.pluralize(extra, 'other');
} else {
limitIndex = -1;
limitStr = ', and ' + items[numItems - 1];
limitStr = ', ' + conjunction + ' ' + items[numItems - 1];
}

return items.slice(0, limitIndex).join(', ') + limitStr;
Expand Down
2 changes: 1 addition & 1 deletion dist/humanize.min.js

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

8 changes: 4 additions & 4 deletions src/humanize.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,21 +326,21 @@
},

// Converts a list of items to a human readable string with an optional limit.
oxford(items, limit, limitStr) {
oxford(items, limit, limitStr, conjunction = 'and') {
const numItems = items.length;

let limitIndex;
if (numItems < 2) {
return String(items);
} else if (numItems === 2) {
return items.join(' and ');
return items.join(` ${conjunction} `);
} else if (exists(limit) && numItems > limit) {
const extra = numItems - limit;
limitIndex = limit;
limitStr = exists(limitStr) ? limitStr : `, and ${extra} ${Humanize.pluralize(extra, 'other')}`;
limitStr = exists(limitStr) ? limitStr : `, ${conjunction} ${extra} ${Humanize.pluralize(extra, 'other')}`;
} else {
limitIndex = -1;
limitStr = `, and ${items[numItems - 1]}`;
limitStr = `, ${conjunction} ${items[numItems - 1]}`;
}

return items.slice(0, limitIndex).join(', ') + limitStr;
Expand Down