Comparing version 1.0.0 to 2.0.0
// Type definitions for cf-prefs | ||
// Project: cf-prefs | ||
export function getPreferenceValue(key: string, type: 'string'): string | undefined; | ||
export function getPreferenceValue(key: string, type: 'string', defaultValue: string): string; | ||
export function getPreferenceValue(key: string, type: 'integer'): number | undefined; | ||
export function getPreferenceValue(key: string, type: 'integer', defaultValue: number): number; | ||
export function getPreferenceValue(key: string, type: 'boolean'): boolean | undefined; | ||
export function getPreferenceValue(key: string, type: 'boolean', defaultValue: boolean): boolean; | ||
export type CFPrefValue = boolean | number | string | Array<CFPrefValue> | Record<string, CFPrefValue> | undefined; | ||
export function getPreferenceValue(key: string): CFPrefValue; | ||
export function isPreferenceForced(key: string): boolean; |
22
index.js
const preferences = require('bindings')('cf-prefs.node'); | ||
function maybeWithDefault(value, defaultValue) { | ||
if (value === undefined && defaultValue !== undefined) { | ||
return defaultValue; | ||
} | ||
return value; | ||
} | ||
function getPreferenceValue(key, type, defaultValue) { | ||
switch (type) { | ||
case 'string': | ||
return maybeWithDefault(preferences.getString(key), defaultValue); | ||
case 'integer': | ||
return maybeWithDefault(preferences.getInteger(key), defaultValue); | ||
case 'boolean': | ||
return maybeWithDefault(preferences.getBoolean(key), defaultValue); | ||
default: | ||
throw new Error(`Unsupported preference type "${type}"`); | ||
} | ||
} | ||
module.exports = { | ||
getPreferenceValue, | ||
getPreferenceValue: preferences.getValue, | ||
isPreferenceForced: preferences.isPreferenceForced, | ||
}; |
{ | ||
"name": "cf-prefs", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "A native node module to access managed app preferences on macOS", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -14,12 +14,9 @@ [![MIT license](https://img.shields.io/badge/License-MIT-blue.svg)](https://lbesson.mit-license.org/) | ||
## `prefs.getPreferenceValue(key, type)` | ||
## `prefs.getPreferenceValue(key)` | ||
* `key` String - The preference key to fetch the value of, has to be a valid UTF-8 string. | ||
* `type` String - The data-type of the preference you are fetching. Can be one of `string`, `integer` or `boolean`. | ||
* `defaultValue` String | Integer | Boolean (Optional) - If no value is present this value will be returned instead, if no default value is provided this method may return `undefined`. | ||
Returns `String` | `Integer` | `Boolean` | `undefined` - Depending on the requested type, see [the type definitions](./index.d.ts) for a more accurate mapping. | ||
Returns `String` | `Integer` | `Boolean` | `Object` | `Array` | `undefined` | ||
**Notes:** | ||
* If the preference is available but in a different type you will not receive an error, you will receive `undefined`. | ||
* If the preference is not available you will receive `undefined`. | ||
@@ -29,3 +26,3 @@ | ||
```js | ||
console.log('Preference Value:', prefs.getPreferenceValue('MyAppsCoolPreference', 'string', 'this-is-the-default')) | ||
console.log('My Preference Value:', prefs.getPreferenceValue('MyAppsCoolPreference')) | ||
``` | ||
@@ -32,0 +29,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12546
10
33
2