What is ssf?
The ssf npm package is a format library for handling spreadsheet string formats. It allows users to format numbers and dates in a way that is consistent with spreadsheet software like Microsoft Excel or Google Sheets. This can be particularly useful for applications that deal with data export, reporting, or any form of data presentation that requires spreadsheet-like formatting.
What are ssf's main functionalities?
Number Formatting
This feature allows you to format numbers according to a specified format string. In this example, a number is formatted to two decimal places.
"SSF.format('0.00', 123.456); // '123.46'"
Date Formatting
This feature enables the formatting of JavaScript Date objects into a string format based on the provided format string. Here, a date is formatted into a YYYY-MM-DD format.
"SSF.format('YYYY-MM-DD', new Date(2023, 3, 1)); // '2023-04-01'"
Custom Formats
SSF allows for the creation of custom formats to handle specific formatting needs. This example demonstrates how to format numbers into a 'Million' or 'Thousand' string based on their value.
"SSF.format('[>999999]#,##0,"Million";#,##0,"Thousand"', 1200000); // '1,200,000,"Million"'"
Other packages similar to ssf
numbro
Numbro is a robust formatting library for numbers. It offers extensive options for number formatting, including currency, percentages, and time spans. While it provides similar number formatting capabilities, it lacks the direct spreadsheet-style formatting and date handling that ssf offers.
date-fns
date-fns provides a comprehensive toolkit for date manipulation and formatting. It excels in handling dates and times, offering a wide range of functions for these purposes. Unlike ssf, date-fns focuses exclusively on dates and does not cover number formatting.
accounting
The accounting package is focused on formatting numbers for financial contexts, offering functions for currency formatting, precision rounding, and number formatting. While it shares the number formatting aspect with ssf, it is more narrowly focused on financial applications and does not include date formatting capabilities.
SSF
SpreadSheet Format (SSF) is a pure-JS library to format data using ECMA-376
spreadsheet format codes (like those used in Microsoft Excel)
This is written in voc -- see ssf.md for code.
To build: voc ssf.md
Setup
In the browser:
<script src="ssf.js"></script>
In node:
var SSF = require('ssf');
Usage
.load(fmt, idx)
sets custom formats (generally indices above 164
)
.format(fmt, val)
formats val
using the format fmt
. If fmt
is of type
number
, the internal table (and custom formats) will be used. If fmt
is a
literal format, then it will be parsed and evaluated.
Notes
Format code 14 in the spec is broken; the correct format is 'mm/dd/yy' (dashes,
not spaces)
License
Apache 2.0