Skip to content

Main content

Sum Calculator Source Code

Browse the implementation files for Sum Calculator.
@tool.io/tools/arithmetic/sum source ↗
tool.ts typescript
import {
  sumParsedValues,
  type SumInterpretation,
} from '@tool.io/tools/arithmetic/sum';

export const sum = (
  input: unknown,
  interpretationInput: unknown,
): string => {
  if (typeof input !== 'string') return '';
  if (
    interpretationInput !== 'numbers' &&
    interpretationInput !== 'numbers-and-ascii'
  ) {
    return '';
  }
  const result = sumParsedValues(
    input,
    interpretationInput as SumInterpretation,
  );
  return result === null ? '' : String(result);
};

export const Tool: TypeTool = {
  name: 'Sum Calculator',
  slug: 'arithmetic/sum',
  intro: 'Calculate the sum of a comma- or line-separated list of values.',
  tool: sum,
  i: [
    {
      example: '1, 2, 3, 4, 5',
      help: 'Enter values separated by commas or new lines. Empty entries are ignored.',
      label: 'Values',
      placeholder: 'Enter values to add',
      type: 'text',
      render: 'textarea',
    },
    {
      label: 'Value Interpretation',
      placeholder: 'Select how values are interpreted',
      type: 'select',
      options: [
        {
          value: 'numbers',
          text: 'Numbers only',
          selected: true,
        },
        {
          value: 'numbers-and-ascii',
          text: 'Numbers + A-Z as ASCII',
        },
      ],
    },
  ],
  o: [
    {
      example: '15',
      help: 'The sum of the parsed values.',
      label: 'Sum',
      placeholder: 'Sum will appear here...',
      type: 'number',
      render: 'textarea',
    },
  ],
  equation: [
    { name: 'Sum', eq: '$$S=\\sum_{i=1}^{n}x_i$$' },
    {
      name: 'ASCII interpretation',
      eq: '$$x_i=\\operatorname{ord}(c_i)$$',
    },
  ],
};

export default Tool;

Description

Browse the implementation files for Sum Calculator.

References

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

Similar or alternative tools