New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

resolve-accept-language

Package Overview
Dependencies
Maintainers
1
Versions
174
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

resolve-accept-language - npm Package Compare versions

Comparing version 1.1.14 to 1.1.15

12

lib/locale-list.d.ts
import Locale from './locale';
export default class LocaleList {
/** A set of ISO 3166-1 alpha-2 country codes. */
readonly countries: Set<string>;
/** A set of ISO 639-1 alpha-2 language codes. */
readonly languages: Set<string>;
/** A set of locale identifiers using the BCP 47 `language`-`country` case-normalized format. */
readonly locales: Set<string>;
/** A list of locale objects. */
readonly objects: Locale[];
/** A set of locale identifiers using the BCP 47 `language`-`country` case-normalized format. */
readonly locales: Set<string>;
/** A set of ISO 639-1 alpha-2 language codes. */
readonly languages: Set<string>;
/** A set of ISO 3166-1 alpha-2 country codes. */
readonly countries: Set<string>;
/**

@@ -12,0 +12,0 @@ * Create a list of locale identifiers.

@@ -14,10 +14,10 @@ "use strict";

var _this = this;
/** A set of ISO 3166-1 alpha-2 country codes. */
this.countries = new Set();
/** A set of ISO 639-1 alpha-2 language codes. */
this.languages = new Set();
/** A set of locale identifiers using the BCP 47 `language`-`country` case-normalized format. */
this.locales = new Set();
/** A list of locale objects. */
this.objects = [];
/** A set of locale identifiers using the BCP 47 `language`-`country` case-normalized format. */
this.locales = new Set();
/** A set of ISO 639-1 alpha-2 language codes. */
this.languages = new Set();
/** A set of ISO 3166-1 alpha-2 country codes. */
this.countries = new Set();
locales.forEach(function (locale) {

@@ -24,0 +24,0 @@ var localeObject = new locale_1.default(locale);

/** Class to manage a locale identifier using the BCP 47 `language`-`country` format. */
export default class Locale {
/** The ISO 3166-1 alpha-2 country code. */
readonly countryCode: string;
/** The locale identifier using the BCP 47 `language`-`country` case-normalized format. */

@@ -7,11 +9,17 @@ readonly identifier: string;

readonly languageCode: string;
/** The ISO 3166-1 alpha-2 country code. */
readonly countryCode: string;
/**
* Is a given string a locale identifier following the BCP 47 `language`-`country` format.
* Create a new `Locale` object.
*
* @param identifier - A potential locale identify to verify.
* @param identifier - A locale identifier using the BCP 47 `language`-`country` format (case insensitive).
*
* @throws An error if the `identifier` format is invalid.
*/
constructor(identifier: string);
/**
* Is a given string an ISO 3166-1 alpha-2 country code.
*
* @param countryCode - An ISO 3166-1 alpha-2 country code.
* @param caseNormalized - Should we verify if the identifier is using the case-normalized format?
*/
static isLocale(identifier: string, caseNormalized?: boolean): boolean;
static isCountryCode(countryCode: string, caseNormalized?: boolean): boolean;
/**

@@ -25,16 +33,8 @@ * Is a given string an ISO 639-1 alpha-2 language code.

/**
* Is a given string an ISO 3166-1 alpha-2 country code.
* Is a given string a locale identifier following the BCP 47 `language`-`country` format.
*
* @param countryCode - An ISO 3166-1 alpha-2 country code.
* @param identifier - A potential locale identify to verify.
* @param caseNormalized - Should we verify if the identifier is using the case-normalized format?
*/
static isCountryCode(countryCode: string, caseNormalized?: boolean): boolean;
/**
* Create a new `Locale` object.
*
* @param identifier - A locale identifier using the BCP 47 `language`-`country` format (case insensitive).
*
* @throws An error if the `identifier` format is invalid.
*/
constructor(identifier: string);
static isLocale(identifier: string, caseNormalized?: boolean): boolean;
}

@@ -22,11 +22,11 @@ "use strict";

/**
* Is a given string a locale identifier following the BCP 47 `language`-`country` format.
* Is a given string an ISO 3166-1 alpha-2 country code.
*
* @param identifier - A potential locale identify to verify.
* @param countryCode - An ISO 3166-1 alpha-2 country code.
* @param caseNormalized - Should we verify if the identifier is using the case-normalized format?
*/
Locale.isLocale = function (identifier, caseNormalized) {
Locale.isCountryCode = function (countryCode, caseNormalized) {
if (caseNormalized === void 0) { caseNormalized = true; }
var regExp = new RegExp(/^[a-z]{2}-[A-Z]{2}$/, caseNormalized ? undefined : 'i');
return regExp.test(identifier);
var regExp = new RegExp(/^[A-Z]{2}$/, caseNormalized ? undefined : 'i');
return regExp.test(countryCode);
};

@@ -45,11 +45,11 @@ /**

/**
* Is a given string an ISO 3166-1 alpha-2 country code.
* Is a given string a locale identifier following the BCP 47 `language`-`country` format.
*
* @param countryCode - An ISO 3166-1 alpha-2 country code.
* @param identifier - A potential locale identify to verify.
* @param caseNormalized - Should we verify if the identifier is using the case-normalized format?
*/
Locale.isCountryCode = function (countryCode, caseNormalized) {
Locale.isLocale = function (identifier, caseNormalized) {
if (caseNormalized === void 0) { caseNormalized = true; }
var regExp = new RegExp(/^[A-Z]{2}$/, caseNormalized ? undefined : 'i');
return regExp.test(countryCode);
var regExp = new RegExp(/^[a-z]{2}-[A-Z]{2}$/, caseNormalized ? undefined : 'i');
return regExp.test(identifier);
};

@@ -56,0 +56,0 @@ return Locale;

@@ -20,17 +20,22 @@ /** Lookup list used to match the preferred locale based on the value of an `Accept-Language` HTTP header. */

/**
* Get a directive object from a directive string.
* Get the top (highest-ranked) locale by language.
*
* @param directiveString - The string representing a directive, extracted from the HTTP header.
* @param languageCode - An ISO 639-1 alpha-2 language code.
*
* @returns A `Directive` object or `undefined` if the string's format is invalid.
* @returns The top locale with the specified language.
*/
private getDirective;
getTopByLanguage(languageCode: string): string | undefined;
/**
* Add a locale in the data object matching its quality.
* Get the top (highest-ranked) locale or language.
*
* @param quality - The HTTP header's quality factor associated with a locale.
* @param identifier - A locale identifier using the BCP 47 `language`-`country` case-normalized format.
* @returns The top match, which can either be a locale or a language.
*/
private addLocale;
getTopLocaleOrLanguage(): string | undefined;
/**
* Get the top (highest-ranked) related locale.
*
* @returns The top related locale.
*/
getTopRelatedLocale(): string | undefined;
/**
* Add a language in the data object matching its quality.

@@ -43,2 +48,9 @@ *

/**
* Add a locale in the data object matching its quality.
*
* @param quality - The HTTP header's quality factor associated with a locale.
* @param identifier - A locale identifier using the BCP 47 `language`-`country` case-normalized format.
*/
private addLocale;
/**
* Add a related locale's language in the data object matching its quality.

@@ -51,2 +63,10 @@ *

/**
* Get a directive object from a directive string.
*
* @param directiveString - The string representing a directive, extracted from the HTTP header.
*
* @returns A `Directive` object or `undefined` if the string's format is invalid.
*/
private getDirective;
/**
* Get the top (highest-ranked) entry from a dataset object entries.

@@ -59,22 +79,2 @@ *

private getTop;
/**
* Get the top (highest-ranked) locale or language.
*
* @returns The top match, which can either be a locale or a language.
*/
getTopLocaleOrLanguage(): string | undefined;
/**
* Get the top (highest-ranked) locale by language.
*
* @param languageCode - An ISO 639-1 alpha-2 language code.
*
* @returns The top locale with the specified language.
*/
getTopByLanguage(languageCode: string): string | undefined;
/**
* Get the top (highest-ranked) related locale.
*
* @returns The top related locale.
*/
getTopRelatedLocale(): string | undefined;
}

@@ -49,40 +49,38 @@ "use strict";

/**
* Get a directive object from a directive string.
* Get the top (highest-ranked) locale by language.
*
* @param directiveString - The string representing a directive, extracted from the HTTP header.
* @param languageCode - An ISO 639-1 alpha-2 language code.
*
* @returns A `Directive` object or `undefined` if the string's format is invalid.
* @returns The top locale with the specified language.
*/
LookupList.prototype.getDirective = function (directiveString) {
/**
* The regular expression is excluding certain directives due to the inability to configure those options in modern
* browsers today (also those options seem unpractical):
*
* - The wildcard character "*", as per RFC 2616 (section 14.4), should match any unmatched language tag.
* - Language tags that starts with a wildcard (e.g. "*-CA") should match the first supported locale of a country.
* - A quality value equivalent to "0", as per RFC 2616 (section 3.9), should be considered as "not acceptable".
*/
var directiveMatch = directiveString.match(/^((?<matchedLanguageCode>([A-Z]{2}))(-(?<matchedCountryCode>[A-Z]{2}))?)(;q=(?<matchedQuality>1|0.(\d*[1-9]\d*){1,3}))?$/i);
if (!(directiveMatch === null || directiveMatch === void 0 ? void 0 : directiveMatch.groups))
return undefined; // No regular expression match.
var _a = directiveMatch.groups, matchedLanguageCode = _a.matchedLanguageCode, matchedCountryCode = _a.matchedCountryCode, matchedQuality = _a.matchedQuality;
var languageCode = matchedLanguageCode.toLowerCase();
var countryCode = matchedCountryCode ? matchedCountryCode.toUpperCase() : undefined;
var quality = matchedQuality === undefined ? '1' : parseFloat(matchedQuality).toString(); // Remove trailing zeros.
var locale = countryCode ? "".concat(languageCode, "-").concat(countryCode) : undefined;
return { locale: locale, languageCode: languageCode, quality: quality };
LookupList.prototype.getTopByLanguage = function (languageCode) {
var _a;
return (_a = this.localeList.objects.find(function (locale) { return locale.languageCode === languageCode; })) === null || _a === void 0 ? void 0 : _a.identifier;
};
/**
* Add a locale in the data object matching its quality.
* Get the top (highest-ranked) locale or language.
*
* @param quality - The HTTP header's quality factor associated with a locale.
* @param identifier - A locale identifier using the BCP 47 `language`-`country` case-normalized format.
* @returns The top match, which can either be a locale or a language.
*/
LookupList.prototype.addLocale = function (quality, identifier) {
if (!this.localesAndLanguagesByQuality[quality]) {
this.localesAndLanguagesByQuality[quality] = new Set();
LookupList.prototype.getTopLocaleOrLanguage = function () {
var localesAndLanguagesByQuality = Object.entries(this.localesAndLanguagesByQuality);
if (localesAndLanguagesByQuality.length === 0) {
return undefined;
}
this.localesAndLanguagesByQuality[quality].add(identifier);
return this.getTop(localesAndLanguagesByQuality);
};
/**
* Get the top (highest-ranked) related locale.
*
* @returns The top related locale.
*/
LookupList.prototype.getTopRelatedLocale = function () {
var relatedLocaleLanguagesByQuality = Object.entries(this.relatedLocaleLanguagesByQuality);
if (relatedLocaleLanguagesByQuality.length === 0) {
return undefined;
}
var topRelatedLocaleLanguage = this.getTop(relatedLocaleLanguagesByQuality);
return this.getTopByLanguage(topRelatedLocaleLanguage);
};
/**
* Add a language in the data object matching its quality.

@@ -100,2 +98,14 @@ *

/**
* Add a locale in the data object matching its quality.
*
* @param quality - The HTTP header's quality factor associated with a locale.
* @param identifier - A locale identifier using the BCP 47 `language`-`country` case-normalized format.
*/
LookupList.prototype.addLocale = function (quality, identifier) {
if (!this.localesAndLanguagesByQuality[quality]) {
this.localesAndLanguagesByQuality[quality] = new Set();
}
this.localesAndLanguagesByQuality[quality].add(identifier);
};
/**
* Add a related locale's language in the data object matching its quality.

@@ -113,2 +123,28 @@ *

/**
* Get a directive object from a directive string.
*
* @param directiveString - The string representing a directive, extracted from the HTTP header.
*
* @returns A `Directive` object or `undefined` if the string's format is invalid.
*/
LookupList.prototype.getDirective = function (directiveString) {
/**
* The regular expression is excluding certain directives due to the inability to configure those options in modern
* browsers today (also those options seem unpractical):
*
* - The wildcard character "*", as per RFC 2616 (section 14.4), should match any unmatched language tag.
* - Language tags that starts with a wildcard (e.g. "*-CA") should match the first supported locale of a country.
* - A quality value equivalent to "0", as per RFC 2616 (section 3.9), should be considered as "not acceptable".
*/
var directiveMatch = directiveString.match(/^((?<matchedLanguageCode>([a-z]{2}))(-(?<matchedCountryCode>[a-z]{2}))?)(;q=(?<matchedQuality>1|0.(\d*[1-9]\d*){1,3}))?$/i);
if (!(directiveMatch === null || directiveMatch === void 0 ? void 0 : directiveMatch.groups))
return undefined; // No regular expression match.
var _a = directiveMatch.groups, matchedLanguageCode = _a.matchedLanguageCode, matchedCountryCode = _a.matchedCountryCode, matchedQuality = _a.matchedQuality;
var languageCode = matchedLanguageCode.toLowerCase();
var countryCode = matchedCountryCode ? matchedCountryCode.toUpperCase() : undefined;
var quality = matchedQuality === undefined ? '1' : Number.parseFloat(matchedQuality).toString(); // Remove trailing zeros.
var locale = countryCode ? "".concat(languageCode, "-").concat(countryCode) : undefined;
return { languageCode: languageCode, locale: locale, quality: quality };
};
/**
* Get the top (highest-ranked) entry from a dataset object entries.

@@ -123,40 +159,4 @@ *

};
/**
* Get the top (highest-ranked) locale or language.
*
* @returns The top match, which can either be a locale or a language.
*/
LookupList.prototype.getTopLocaleOrLanguage = function () {
var localesAndLanguagesByQuality = Object.entries(this.localesAndLanguagesByQuality);
if (!localesAndLanguagesByQuality.length) {
return undefined;
}
return this.getTop(localesAndLanguagesByQuality);
};
/**
* Get the top (highest-ranked) locale by language.
*
* @param languageCode - An ISO 639-1 alpha-2 language code.
*
* @returns The top locale with the specified language.
*/
LookupList.prototype.getTopByLanguage = function (languageCode) {
var _a;
return (_a = this.localeList.objects.find(function (locale) { return locale.languageCode === languageCode; })) === null || _a === void 0 ? void 0 : _a.identifier;
};
/**
* Get the top (highest-ranked) related locale.
*
* @returns The top related locale.
*/
LookupList.prototype.getTopRelatedLocale = function () {
var relatedLocaleLanguagesByQuality = Object.entries(this.relatedLocaleLanguagesByQuality);
if (!relatedLocaleLanguagesByQuality.length) {
return undefined;
}
var topRelatedLocaleLanguage = this.getTop(relatedLocaleLanguagesByQuality);
return this.getTopByLanguage(topRelatedLocaleLanguage);
};
return LookupList;
}());
exports.default = LookupList;
/** Resolve the preferred locale from an HTTP `Accept-Language` header. */
export declare class ResolveAcceptLanguage {
/** The language-based match, if applicable. */
private languageBasedMatch;
/** The locale-based match, if applicable. */
private localeBasedMatch;
/** The language-based match, if applicable. */
private languageBasedMatch;
/** The related-locale-based match, if applicable. */

@@ -20,14 +20,8 @@ private relatedLocaleBasedMatch;

/**
* Was a match found when resolving the preferred locale?
* Is the best match language-based?
*
* @returns True when a match is found, otherwise false.
* @returns True if the best match language-based, otherwise false.
*/
hasMatch(): boolean;
bestMatchIsLanguageBased(): boolean;
/**
* Did the resolution of the preferred locale find no match?
*
* @returns True when there is no match, otherwise false.
*/
hasNoMatch(): boolean;
/**
* Is the best match locale-based?

@@ -39,8 +33,2 @@ *

/**
* Is the best match language-based?
*
* @returns True if the best match language-based, otherwise false.
*/
bestMatchIsLanguageBased(): boolean;
/**
* Is the best match related-locale-based?

@@ -57,2 +45,14 @@ *

getBestMatch(): string | undefined;
/**
* Was a match found when resolving the preferred locale?
*
* @returns True when a match is found, otherwise false.
*/
hasMatch(): boolean;
/**
* Did the resolution of the preferred locale find no match?
*
* @returns True when there is no match, otherwise false.
*/
hasNoMatch(): boolean;
}

@@ -59,0 +59,0 @@ /**

@@ -42,23 +42,10 @@ "use strict";

/**
* Was a match found when resolving the preferred locale?
* Is the best match language-based?
*
* @returns True when a match is found, otherwise false.
* @returns True if the best match language-based, otherwise false.
*/
ResolveAcceptLanguage.prototype.hasMatch = function () {
if (this.localeBasedMatch !== undefined ||
this.languageBasedMatch !== undefined ||
this.relatedLocaleBasedMatch !== undefined) {
return true;
}
return false;
ResolveAcceptLanguage.prototype.bestMatchIsLanguageBased = function () {
return this.languageBasedMatch !== undefined;
};
/**
* Did the resolution of the preferred locale find no match?
*
* @returns True when there is no match, otherwise false.
*/
ResolveAcceptLanguage.prototype.hasNoMatch = function () {
return !this.hasMatch();
};
/**
* Is the best match locale-based?

@@ -72,10 +59,2 @@ *

/**
* Is the best match language-based?
*
* @returns True if the best match language-based, otherwise false.
*/
ResolveAcceptLanguage.prototype.bestMatchIsLanguageBased = function () {
return this.languageBasedMatch !== undefined;
};
/**
* Is the best match related-locale-based?

@@ -94,13 +73,21 @@ *

ResolveAcceptLanguage.prototype.getBestMatch = function () {
if (this.localeBasedMatch !== undefined) {
return this.localeBasedMatch;
}
else if (this.languageBasedMatch !== undefined) {
return this.languageBasedMatch;
}
else if (this.relatedLocaleBasedMatch !== undefined) {
return this.relatedLocaleBasedMatch;
}
return undefined;
var _a, _b;
return (_b = (_a = this.localeBasedMatch) !== null && _a !== void 0 ? _a : this.languageBasedMatch) !== null && _b !== void 0 ? _b : this.relatedLocaleBasedMatch;
};
/**
* Was a match found when resolving the preferred locale?
*
* @returns True when a match is found, otherwise false.
*/
ResolveAcceptLanguage.prototype.hasMatch = function () {
return this.getBestMatch() !== undefined ? true : false;
};
/**
* Did the resolution of the preferred locale find no match?
*
* @returns True when there is no match, otherwise false.
*/
ResolveAcceptLanguage.prototype.hasNoMatch = function () {
return !this.hasMatch();
};
return ResolveAcceptLanguage;

@@ -107,0 +94,0 @@ }());

{
"name": "resolve-accept-language",
"version": "1.1.14",
"version": "1.1.15",
"description": "Resolve the preferred locale based on the value of an `Accept-Language` HTTP header.",

@@ -13,4 +13,5 @@ "author": "Avansai (https://avansai.com)",

"scripts": {
"build": "rm -Rf ./lib && tsc && npm run lint && npm test",
"lint": "eslint --ext .js --ext .jsx --ext .ts --ext .tsx --fix .",
"build": "npm run prettier && npm run lint-fix && rm -Rf ./lib && tsc && npm test",
"lint-fix": "eslint --ext .js --ext .jsx --ext .ts --ext .tsx --fix .",
"lint-check": "eslint --ext .js --ext .jsx --ext .ts --ext .tsx .",
"lint-print-config": "eslint --print-config ./eslintrc.yaml",

@@ -42,16 +43,18 @@ "prettier": "prettier --write .",

"@types/jest": "^28.1.6",
"@typescript-eslint/eslint-plugin": "^5.31.0",
"@typescript-eslint/parser": "^5.31.0",
"@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.33.0",
"dotenv-cli": "^6.0.0",
"eslint": "^8.20.0",
"eslint": "^8.21.0",
"eslint-config-prettier": "^8.5.0",
"eslint-import-resolver-node": "^0.3.6",
"eslint-import-resolver-typescript": "^3.3.0",
"eslint-import-resolver-typescript": "^3.4.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^26.7.0",
"eslint-plugin-jest": "^26.8.2",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-unicorn": "^43.0.2",
"jest": "^28.1.3",
"prettier": "^2.7.1",
"prettier-plugin-organize-imports": "^3.0.0",
"release-it": "^15.2.0",
"prettier-plugin-organize-imports": "^3.0.3",
"prettier-plugin-sh": "^0.12.8",
"release-it": "^15.3.0",
"ts-jest": "^28.0.7",

@@ -58,0 +61,0 @@ "ts-node": "^10.9.1",

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc