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

Adds strict type definitions to sprintf #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions packages/sprintf/.gitignore
aduth marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!types/*
25 changes: 9 additions & 16 deletions packages/sprintf/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,19 @@ var PATTERN =
* sprintf( 'Hello %s!', 'world' );
* // ⇒ 'Hello world!'
* ```
*
* @param {string} string printf format string
* @param {...string|string[]|Object<string,string>} [args] String arguments.
* @template {string} T
* @param {T} string - string printf format string
* @param {import('./types/index.d').SprintfArgs<T>|import('./types/index.d').SprintfArgs<T>[]} args String arguments.
*
* @return {string} Formatted string.
*/
export default function sprintf(string, args) {
var i;
export default function sprintf(string, ...args) {
var i = 0;

if (!Array.isArray(args)) {
// Construct a copy of arguments from index one, used for replace
aduth marked this conversation as resolved.
Show resolved Hide resolved
// function placeholder substitution.
args = new Array(arguments.length - 1);
for (i = 1; i < arguments.length; i++) {
args[i - 1] = arguments[i];
}
if (Array.isArray(args[0])) {
args = /** @type {import('./types/index.d').SprintfArgs<T>[]} */ args[0];
}

i = 1;

return string.replace(PATTERN, function () {
var index, name, precision, type, value;

Expand All @@ -89,14 +82,14 @@ export default function sprintf(string, args) {

// Asterisk precision determined by peeking / shifting next argument.
if (precision === '*') {
precision = args[i - 1];
precision = args[i];
i++;
}

if (name === undefined) {
// If not a positional argument, use counter value.
if (index === undefined) {
index = i;
index = i + 1;
}

i++;
Expand Down
25 changes: 25 additions & 0 deletions packages/sprintf/types/index.d.ts
erikyo marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
type Specifiers = {
's': string,
'd': number,
'f': number
};
type S = keyof Specifiers;

type ExtractNamedPlaceholders<T extends string> =
T extends `${any}%(${infer Key})${infer Spec}${infer Rest}`
? Spec extends S
? { [K in Key]: Specifiers[Spec]} & ExtractNamedPlaceholders<Rest>
: never
: {};

type ExtractUnnamedPlaceholders<T extends string> =
T extends `${any}%${infer Spec}${infer Rest}`
? Spec extends S
? [Specifiers[Spec], ...ExtractUnnamedPlaceholders<Rest>]
: never
: [];

export type SprintfArgs<T extends string> =
ExtractUnnamedPlaceholders<T> extends never
? [values: ExtractNamedPlaceholders<T>]
: ExtractUnnamedPlaceholders<T>;
Loading