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

Fix format function types - improve requireLocale error message #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Formats difference between two dates as a human-readable string in almost any la

### Node.js

In Node.js environment library will load requested locales automatically.
In Node.js environment library will load requested locales automatically from a relative path.

```js
const timeDelta = require('time-delta');
Expand All @@ -63,20 +63,27 @@ This ensures minimal size of your application bundle.
```js
// Importing the library
import * as timeDelta from 'time-delta';
import * as numerous from 'numerous';

// Importing locales that you want to use
import enLocale from 'time-delta/locales/en';
import numerousEnLocale from 'numerous/locales/en';
import ruLocale from 'time-delta/locales/ru';
import numerousRuLocale from 'numerous/locales/ru';

// Registering locale
timeDelta.addLocale(enLocale);
numerous.addLocale(numerousEnLocale);


// You can register multiple locales
timeDelta.addLocale([enLocale, ruLocale]);
timeDelta.addLocale([numerousEnLocale, numerousRuLocale]);

// Creating an instance
const instance = timeDelta.create({
locale: 'en', // default
locale: 'en',
autoloadLocales: false
});

const date1 = new Date('2015-04-01T21:00:00');
Expand Down
4 changes: 3 additions & 1 deletion lib/time-delta.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ function requireLocale(localeId) {
);

} catch (error) {
throw Error(`Failed to load locale: ${localeId}`);
throw Error(
`Failed to load locale: ${localeId} from ../locales/${localeId}.js. If using a bundled time-delta, set 'autoloadLocales: false' in the config: ${error}`
);

}
}
Expand Down
9 changes: 8 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ export interface Locale {
}

export interface Formatter {
format(firstDate: Date, secondDate: Date, options?: Config);
/**
* Returns difference between two dates as a text string.
*/
format(firstDate: Date | number, secondDate: Date | number, options?: Config): string;
}

export function create(config?: Config): Formatter;

/**
* Adds pluralization data for the specified locale.
* Should be called in browser.
*/
export function addLocale(localeData: (Locale | Locale[]));

export const defaultConfig: Config;