Skip to content

Commit

Permalink
TypeScript likes lower case for primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraq committed Apr 28, 2022
1 parent a4097b3 commit 627b267
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 42 deletions.
15 changes: 15 additions & 0 deletions source/IcuMessageFormatter.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export {default as MessageFormatter} from './MessageFormatter.js';
export {default as pluralTypeHandler} from './pluralTypeHandler.js';
Expand Down
20 changes: 10 additions & 10 deletions source/MessageFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ import {memoize} from '@ultraq/function-utils';

/**
* @callback FormatFunction
* @param {String} message
* @param {string} message
* @param {FormatValues} [values={}]
* @return {String}
* @return {string}
*/

/**
* @template T
* @callback TypeHandler<T>
* @param {String} value
* @param {String} matches
* @param {String} locale
* @param {string} value
* @param {string} matches
* @param {string} locale
* @param {FormatValues} values
* @param {FormatFunction} format
* @return T
* @return {T}
*/

/**
Expand All @@ -52,7 +52,7 @@ export default class MessageFormatter {
* Creates a new formatter that can work using any of the custom type handlers
* you register.
*
* @param {String} locale
* @param {string} locale
* @param {Record<string,TypeHandler<*>>} [typeHandlers={}]
* Optional object where the keys are the names of the types to register,
* their values being the functions that will return a nicely formatted
Expand All @@ -68,9 +68,9 @@ export default class MessageFormatter {
* Formats an ICU message syntax string using `values` for placeholder data
* and any currently-registered type handlers.
*
* @param {String} message
* @param {string} message
* @param {FormatValues} [values={}]
* @return {String}
* @return {string}
*/
format = memoize((message, values = {}) => {

Expand All @@ -88,7 +88,7 @@ export default class MessageFormatter {
* This method is used by {@link MessageFormatter#format} where it acts as a
* string renderer.
*
* @param {String} message
* @param {string} message
* @param {FormatValues} [values={}]
* @return {Array<any>}
*/
Expand Down
14 changes: 7 additions & 7 deletions source/pluralTypeHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ const OTHER = 'other';

/**
* @private
* @param {String} caseBody
* @param {Number} value
* @return {Object} {caseBody: string, numberValues: object}
* @param {string} caseBody
* @param {number} value
* @return {{caseBody: string, numberValues: object}}
*/
function replaceNumberSign(caseBody, value) {
let i = 0;
Expand Down Expand Up @@ -69,12 +69,12 @@ function replaceNumberSign(caseBody, value) {
* See https://formatjs.io/docs/core-concepts/icu-syntax#plural-format for more
* details on how the `plural` statement works.
*
* @param {String} value
* @param {String} matches
* @param {String} locale
* @param {string} value
* @param {string} matches
* @param {string} locale
* @param {FormatValues} values
* @param {FormatFunction} format
* @return {String}
* @return {string}
*/
export default function pluralTypeHandler(value, matches = '', locale, values, format) {
const { args, cases } = parseCases(matches);
Expand Down
8 changes: 4 additions & 4 deletions source/selectTypeHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ const OTHER = 'other';
* See https://formatjs.io/docs/core-concepts/icu-syntax#select-format for more
* details on how the `select` statement works.
*
* @param {String} value
* @param {String} matches
* @param {String} locale
* @param {string} value
* @param {string} matches
* @param {string} locale
* @param {FormatValues} values
* @param {FormatFunction} format
* @return {String}
* @return {string}
*/
export default function selectTypeHandler(value, matches = '', locale, values, format) {
const { cases } = parseCases(matches);
Expand Down
50 changes: 29 additions & 21 deletions source/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,25 @@
*/

/**
* Most branch-based type handlers are based around "cases".
* For example, `select` and `plural` compare compare a value
* to "case keys" to choose a subtranslation.
* @typedef ParseCasesResult
* @property {string[]} args
* A list of prepended arguments.
* @property {Record<string,string>} cases
* A map of all cases.
*/

/**
* Most branch-based type handlers are based around "cases". For example,
* `select` and `plural` compare compare a value to "case keys" to choose a
* subtranslation.
*
* This util splits "matches" portions provided to the aforementioned
* handlers into case strings, and extracts any prepended arguments
* (for example, `plural` supports an `offset:n` argument used for
* populating the magic `#` variable).
* This util splits "matches" portions provided to the aforementioned handlers
* into case strings, and extracts any prepended arguments (for example,
* `plural` supports an `offset:n` argument used for populating the magic `#`
* variable).
*
* @param {String} string
* @return {Object} The `cases` key points to a map of all cases.
* The `arguments` key points to a list of prepended arguments.
* @param {string} string
* @return {ParseCasesResult}
*/
export function parseCases(string) {
const isWhitespace = ch => /\s/.test(ch);
Expand Down Expand Up @@ -100,10 +107,11 @@ export function parseCases(string) {
* Finds the index of the matching closing curly bracket, including through
* strings that could have nested brackets.
*
* @param {String} string
* @param {Number} fromIndex
* @return {Number} The index of the matching closing bracket, or -1 if no
* closing bracket could be found.
* @param {string} string
* @param {number} fromIndex
* @return {number}
* The index of the matching closing bracket, or -1 if no closing bracket
* could be found.
*/
export function findClosingBracket(string, fromIndex) {
let depth = 0;
Expand All @@ -126,8 +134,8 @@ export function findClosingBracket(string, fromIndex) {
* Split a `{key, type, format}` block into those 3 parts, taking into account
* nested message syntax that can exist in the `format` part.
*
* @param {String} block
* @return {Array}
* @param {string} block
* @return {string[]}
* An array with `key`, `type`, and `format` items in that order, if present
* in the formatted argument block.
*/
Expand All @@ -140,11 +148,11 @@ export function splitFormattedArgument(block) {
* remainder of the string to be grouped together in a final entry.
*
* @private
* @param {String} string
* @param {String} separator
* @param {Number} limit
* @param {Array} [accumulator=[]]
* @return {Array}
* @param {string} string
* @param {string} separator
* @param {number} limit
* @param {string[]} [accumulator=[]]
* @return {string[]}
*/
function split(string, separator, limit, accumulator = []) {
if (!string) {
Expand Down

0 comments on commit 627b267

Please sign in to comment.