Skip to content

Main content

Progressive Tax Calculator Source Code

Browse the implementation files for Progressive Tax Calculator.
@tool.io/tools/tax/progressive source ↗
tool.ts typescript
export type ProgressiveTaxBracket = {
	rate: number;
	upTo: number | null;
};

export type ProgressiveTaxBandResult = {
	from: number;
	rate: number;
	tax: number;
	taxableAmount: number;
	upTo: number | null;
};

export type ProgressiveTaxResult = {
	bands: readonly ProgressiveTaxBandResult[];
	effectiveRate: number;
	income: number;
	kind: 'tax/progressive';
	marginalRate: number;
	tax: number;
	version: 1;
};

export type ProgressiveTaxInput = {
	brackets: ProgressiveTaxBracket[];
	income: number;
};

export const calculateProgressiveTax = (
	input: unknown
): ProgressiveTaxResult | null => {
	const result = calculatePortableProgressiveTax(input);
	if (!result.ok) return null;
	return {
		...result.output,
		kind: 'tax/progressive',
		version: 1
	};
};

const parseBrackets = (input: unknown): unknown => {
	if (typeof input !== 'string') return input;
	try {
		return JSON.parse(input);
	} catch {
		return null;
	}
};

export const progressiveTax = (
	incomeInput: unknown,
	bracketsInput: unknown
): string => {
	const income = Number(incomeInput);
	const result = calculateProgressiveTax({
		brackets: parseBrackets(bracketsInput),
		income
	});
	return result === null ? '' : JSON.stringify(result, null, 2);
};

export const Tool: TypeTool = {
	name: 'Progressive Tax Calculator',
	slug: 'tax/progressive',
	intro:
		'Calculate marginal tax bands, total tax, and effective and marginal rates from an explicit bracket schedule.',
	tool: progressiveTax,
	i: [
		{
			example: '100000',
			label: 'Taxable Income',
			placeholder: 'Enter taxable income',
			type: 'number'
		},
		{
			example:
				'[{"upTo":12000,"rate":0},{"upTo":50000,"rate":0.2},{"upTo":null,"rate":0.4}]',
			help: 'Use decimal rates and null as the upper limit of the final bracket.',
			label: 'Tax Brackets',
			placeholder: 'Enter brackets as JSON',
			render: 'textarea',
			type: 'text'
		}
	],
	o: [
		{
			example:
				'{\n  "tax": 27600,\n  "effectiveRate": 0.276,\n  ...\n}',
			label: 'Progressive Tax',
			placeholder: 'Tax calculation will appear here...',
			render: 'textarea',
			type: 'text'
		}
	],
	cite: [
		{
			title: 'Progressive tax - Wikipedia',
			url: 'https://en.wikipedia.org/wiki/Progressive_tax'
		}
	]
};

export default Tool;
import { calculateProgressiveTax as calculatePortableProgressiveTax } from '@tool.io/tools/tax/progressive';

Description

Browse the implementation files for Progressive Tax Calculator.

References

  1. Tool.io on GitHub
  2. Tool.io TypeScript tools library

Similar or alternative tools