
Security News
The Code You Didn't Write Is Still Yours to Defend
AI agents are pulling packages into environments no scanner is watching, creating exposure before security teams can see it.
@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.
/*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.
The 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: Template, data: Data): ConditionalStringResult<Template, Data>Processes a string template with conditional comments.
Parameters:
template: The string with conditional commentsdata: An object with condition valuesReturns: The processed string with conditionals resolved
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
The npm package @kuindji/conditional-string receives a total of 5 weekly downloads. As such, @kuindji/conditional-string popularity was classified as not popular.
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.

Security News
AI agents are pulling packages into environments no scanner is watching, creating exposure before security teams can see it.

Security News
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.

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