
Product
Introducing Repository Access Permissions and Custom Roles
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.
@kuindji/conditional-string
Advanced tools
Type-safe conditional string template parser with compile-time inference
Type-safe conditional string template parser with compile-time inference for TypeScript.
.with<Data>() to specify static types for return type computation/*if:condition*/.../*endif*/ that works with any string! prefix for negated conditionsuser.isAdmin syntaxnpm install @kuindji/conditional-string
# or
bun add @kuindji/conditional-string
# or
yarn add @kuindji/conditional-string
import { conditionalString } from "@kuindji/conditional-string";
const template = `
Hello /*if:showName*/World/*endif*/!
/*if:showGreeting*/Have a nice day!/*endif*/
`;
const result = conditionalString(
template,
{
showName: true,
showGreeting: false,
} as const,
);
// Result: "Hello World!\n"
// TypeScript knows the exact string type at compile time!
import { conditionalString } from "@kuindji/conditional-string";
const sql = `
SELECT * FROM users
WHERE 1=1
/*if:includeDeleted*/AND deleted = false/*endif*/
/*if:filterActive*/AND active = true/*endif*/
`;
const result = conditionalString(
sql,
{
includeDeleted: true,
filterActive: false,
} as const,
);
// Result: "SELECT * FROM users WHERE 1=1 AND deleted = false"
Use ! to negate a condition:
const template = `Welcome /*if:!isGuest*/back/*endif*/!`;
conditionalString(template, { isGuest: false } as const);
// Result: "Welcome back!"
conditionalString(template, { isGuest: true } as const);
// Result: "Welcome !"
Access nested object properties using dot notation:
const template =
`User: /*if:user.isAdmin*/[ADMIN] /*endif*//*if:user.name*/John/*endif*/`;
conditionalString(template, { user: { isAdmin: true, name: true } } as const);
// Result: "User: [ADMIN] John"
Conditions can be nested arbitrarily:
const template = `Start /*if:a*/A /*if:b*/B/*endif*//*endif*/ End`;
conditionalString(template, { a: true, b: true } as const);
// Result: "Start A B End"
conditionalString(template, { a: true, b: false } as const);
// Result: "Start A End"
conditionalString(template, { a: false, b: true } as const);
// Result: "Start End"
const getUsersQuery = `
SELECT u.id, u.name, u.email
FROM users u
WHERE 1=1
/*if:searchTerm*/AND (u.name ILIKE $1 OR u.email ILIKE $1)/*endif*/
/*if:roleFilter*/AND u.role = $2/*endif*/
/*if:activeOnly*/AND u.active = true/*endif*/
ORDER BY u.created_at DESC
`;
const query = conditionalString(
getUsersQuery,
{
searchTerm: "john",
roleFilter: null,
activeOnly: true,
} as const,
);
// Results in:
// SELECT u.id, u.name, u.email
// FROM users u
// WHERE 1=1
// AND (u.name ILIKE $1 OR u.email ILIKE $1)
// AND u.active = true
// ORDER BY u.created_at DESC
The library provides compile-time type inference. When using literal types (via as const), TypeScript will compute the exact resulting string type:
import { type ConditionalStringResult } from "@kuindji/conditional-string";
// The type is computed at compile time
type Result = ConditionalStringResult<
"Hello /*if:flag*/World/*endif*/!",
{ flag: true; }
>;
// Result = "Hello World!"
type Result2 = ConditionalStringResult<
"Hello /*if:flag*/World/*endif*/!",
{ flag: false; }
>;
// Result2 = "Hello !"
If the condition values are not literal types (e.g., widened boolean instead of true or false), the result type falls back to string.
.with<Data>()When you need to specify a static type for return type computation while allowing runtime values to vary, use the .with<Data>() method:
import { conditionalString } from "@kuindji/conditional-string";
// Runtime value determined dynamically
const showAdmin = Math.random() > 0.5;
const template = `User /*if:isAdmin*/[ADMIN]/*endif*/`;
// Using .with<Data>() to specify static type for return type computation
const result = conditionalString.with<{ isAdmin: true }>()(
template,
{ isAdmin: showAdmin }, // Runtime value can be boolean
);
// Return type is computed as if isAdmin is always true: "User [ADMIN]"
// But runtime result depends on actual showAdmin value
This is useful when:
The .with<Data>() method:
Data for return type computationThe library follows JavaScript truthy/falsy semantics:
false, 0, "", null, undefined[] and empty objects {}/*if:condition*/content/*endif*/
condition: The property name to check (supports dot notation and ! prefix)content: The content to include when the condition is truthyconditionalString<Template, Data>(template, data)Processes a string template with conditional comments. Both Template and Data types are inferred.
Parameters:
template: The string with conditional commentsdata: An object with condition valuesReturns: ConditionalStringResult<Template, Data> - The processed string with conditionals resolved
conditionalString.with<StaticData>()(template, data)Creates a typed version where StaticData is used for return type computation, but runtime data values are unrestricted.
Type Parameters:
StaticData: The type used for computing the return typeParameters:
template: The string with conditional comments (type is inferred)data: An object with keys matching StaticData, but values can be any typeReturns: ConditionalStringResult<Template, StaticData> - The processed string with type computed using StaticData
Example:
// Static type says isAdmin is true, so return type includes "[ADMIN]"
// Runtime data can have isAdmin as any boolean
const result = conditionalString.with<{ isAdmin: true }>()(
`User /*if:isAdmin*/[ADMIN]/*endif*/`,
{ isAdmin: someRuntimeBoolean },
);
ConditionalStringResult<Template, Data>A utility type that computes the resulting string type based on the template and data types.
MIT
FAQs
Type-safe conditional string template parser with compile-time inference
We found that @kuindji/conditional-string demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.

Product
Socket MCP now lets AI assistants review org alerts, investigate threats using the Socket threat feed, and inspect package files in addition to dependency scoring.

Product
Socket Firewall blocks malicious VS Code and Open VSX extensions before install, protecting developers from compromised editor marketplaces.