Gathering detailed insights and metrics for properties-file
Gathering detailed insights and metrics for properties-file
Gathering detailed insights and metrics for properties-file
Gathering detailed insights and metrics for properties-file
.properties file parser, editor, formatter and Webpack loader.
npm install properties-file
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
22 Stars
279 Commits
3 Forks
2 Watching
1 Branches
3 Contributors
Updated on 23 Nov 2024
Minified
Minified + Gzipped
TypeScript (87.82%)
JavaScript (12.18%)
Cumulative downloads
Total Downloads
Last day
3.2%
49,076
Compared to previous day
Last week
-0.8%
262,944
Compared to previous week
Last month
-22.6%
1,153,155
Compared to previous month
Last year
527.2%
5,785,415
Compared to previous year
32
.properties
file parser, editor, formatter and Webpack loader.
โ In April 2023, we released version 3 of this package, which includes breaking changes. Please refer to the upgrade guide before upgrading.
Add the package as a dependency:
npm install properties-file
getProperties
converts the content of .properties
files to a key-value pair object.Properties
class provides insights into parsing data.PropertiesEditor
class enables the addition, edition, and removal of entries.escapeKey
and escapeValue
allow the conversion of any content to a .properties
compatible format..properties
files directly into your application..properties
packages have been inactive for years).We have put a lot of effort into incorporating TSDoc into all our APIs. If you are unsure about how to use certain APIs provided in our examples, please check directly in your IDE.
getProperties
(converting .properties
to an object)The most common use case for .properties
files is for Node.js applications that need to read the file's content into a simple key-value pair object. Here is how this can be done with a single API call:
1import { readFileSync } from 'node:fs' 2import { getProperties } from 'properties-file' 3 4console.log(getProperties(readFileSync('hello-world.properties')))
Output:
1{ hello: 'hello', world: 'world' }
Properties
(using parsing metadata)The Properties
object is what makes getProperties
work under the hood, but when using it directly, you can access granular parsing metadata. Here is an example of how the object can be used to find key collisions:
1import { Properties } from 'properties-file' 2 3const properties = new Properties( 4 'hello = hello1\nworld = world1\nworld = world2\nhello = hello2\nworld = world3' 5) 6console.log(properties.format()) 7 8/** 9 * Outputs: 10 * 11 * hello = hello1 12 * world = world1 13 * world = world2 14 * hello = hello2 15 * world = world3 16 */ 17 18properties.collection.forEach((property) => { 19 console.log(`${property.key} = ${property.value}`) 20}) 21 22/** 23 * Outputs: 24 * 25 * hello = hello2 26 * world = world3 27 */ 28 29const keyCollisions = properties.getKeyCollisions() 30 31keyCollisions.forEach((keyCollision) => { 32 console.warn( 33 `Found a key collision for key '${ 34 keyCollision.key 35 }' on lines ${keyCollision.startingLineNumbers.join( 36 ', ' 37 )} (will use the value at line ${keyCollision.getApplicableLineNumber()}).` 38 ) 39}) 40 41/** 42 * Outputs: 43 * 44 * Found a key collision for key 'hello' on lines 1, 4 (will use the value at line 4). 45 * Found a key collision for key 'world' on lines 2, 3, 5 (will use the value at line 5). 46 */
For purposes where you require more parsing metadata, such as building a syntax highlighter, it is recommended that you access the Property
objects included in the Properties.collection
. These objects provide comprehensive information about each key-value pair.
PropertiesEditor
(editing .properties
content)In certain scenarios, it may be necessary to modify the content of the .properties
key-value pair objects. This can be achieved easily using the Properties
object, with the assistance of the escapeKey
and escapeValue
APIs, as demonstrated below:
1import { Properties } from 'properties-file' 2import { escapeKey, escapeValue } from 'properties-file/escape' 3 4const properties = new Properties('hello = hello\n# This is a comment\nworld = world') 5const newProperties: string[] = [] 6 7properties.collection.forEach((property) => { 8 const key = property.key === 'world' ? 'new world' : property.key 9 const value = property.value === 'world' ? 'new world' : property.value 10 newProperties.push(`${escapeKey(key)} = ${escapeValue(value)}`) 11}) 12 13console.log(newProperties.join('\n')) 14 15/** 16 * Outputs: 17 * 18 * hello = hello 19 * new\ world = new world 20 */
The limitation of this approach is that its output contains only valid keys, without any comments or whitespace. However, if you require a more advanced editor that preserves these original elements, then the PropertiesEditor
object is exactly what you need.
1import { PropertiesEditor } from 'properties-file/editor' 2 3const properties = new PropertiesEditor('hello = hello\n# This is a comment\nworld = world') 4console.log(properties.format()) 5 6/** 7 * Outputs: 8 * 9 * hello = hello 10 * # This is a comment 11 * world = world 12 */ 13 14properties.insertComment('This is a multiline\ncomment before `newKey3`') 15properties.insert('newKey3', 'This is my third key') 16 17properties.insert('newKey1', 'This is my first new key', { 18 referenceKey: 'newKey3', 19 position: 'before', 20 comment: 'Below are the new keys being edited', 21 commentDelimiter: '!', 22}) 23 24properties.insert('newKey2', 'ใใใซใกใฏ', { 25 referenceKey: 'newKey1', 26 position: 'after', 27 escapeUnicode: true, 28}) 29 30properties.delete('hello') 31properties.update('world', { 32 newValue: 'new world', 33}) 34console.log(properties.format()) 35 36/** 37 * Outputs: 38 * 39 * # This is a comment 40 * world = new world 41 * ! Below are the new keys being edited 42 * newKey1 = This is my first new key 43 * newKey2 = \u3053\u3093\u306b\u3061\u306f 44 * # This is a multiline 45 * # comment before `newKey3` 46 * newKey3 = This is my third key 47 */
For convenience, we also added an upsert
method that allows updating a key if it exists or adding it at the end, when it doesn't. Make sure to check in your IDE for all available methods and options in our TSDoc.
If you would like to import .properties
directly using import
, this package comes with its own Webpack file loader located under properties-file/webpack-loader
. Here is an example of how to configure it:
1// webpack.config.js 2module.exports = { 3 module: { 4 rules: [ 5 { 6 test: /\.properties$/i, 7 use: [ 8 { 9 loader: 'properties-file/webpack-loader', 10 }, 11 ], 12 }, 13 ], 14 }, 15}
As soon as you configure Webpack, the .properties
type should be available in your IDE when using import
. If you ever need to add it manually, you can add a *.properties
type declaration file at the root of your application, like this:
1declare module '*.properties' { 2 /** A key/value object representing the content of a `.properties` file. */ 3 const properties: { 4 /** The value of a `.properties` file key. */ 5 [key: string]: string 6 } 7 export { properties } 8}
By adding these configurations you should now be able to import directly .properties
files just like this:
1import { properties as helloWorld } from './hello-world.properties' 2 3console.dir(helloWorld)
Output:
1{ "hello": "world" }
.properties
file package?There are probably over 20 similar packages available, but:
Unfortunately, the .properties
file specification is not well-documented. One reason for this is that it was originally used in Java to store configurations. Today, most applications handle this using JSON, YAML, or other modern formats because these formats are more flexible.
.properties
files?While many options exist today to handle configurations, .properties
files remain one of the best options to store localizable strings (also known as messages). On the Java side, PropertyResourceBundle
is how most implementations handle localization today. Because of its simplicity and maturity, .properties
files remain one of the best options today when it comes to internationalization (i18n):
File format | Key/value based | Supports inline comments | Built for localization | Good linguistic tools support |
---|---|---|---|---|
.properties | Yes | Yes | Yes (Resource Bundles) | Yes |
JSON | No (can do more) | No (requires JSON5) | No | Depends on the schema |
YAML | No (can do more) | Yes | No | Depends on the schema |
Having good JavaScript/TypeScript support for .properties
files offers more internationalization (i18n) options.
Basically, our goal was to offer parity with the Java implementation, which is the closest thing to a specification for .properties
files. Here is the logic behind this package in a nutshell:
Property
objects that:
Just like Java, if a Unicode-escaped character (\u
) is malformed, an error will be thrown. However, we do not recommend using Unicode-escaped characters, but rather using UTF-8 encoding that supports more characters.
Properties
class documentationPropertyResourceBundle
documentationThanks to @calibr, the creator of properties-file version 1.0, for letting us use the https://www.npmjs.com/package/properties-file package name. We hope that it will make it easier to find our package.
No vulnerabilities found.
No security vulnerabilities found.