Skip to content

Commit 02dbd76

Browse files
committed
feat: implement add/remove domain
1 parent 0c3bc60 commit 02dbd76

File tree

4 files changed

+95
-11
lines changed

4 files changed

+95
-11
lines changed

README.md

+37-9
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const { getUserDefault } = require('node-mac-userdefaults')
7373
const interfaceStyle = getUserDefault('AppleInterfaceStyle', 'string')
7474

7575
console.log(interfaceStyle) // 'Dark'
76-
```
76+
```
7777

7878
### `defaults.setUserDefault(type, key, value[, domain])`
7979

@@ -91,6 +91,8 @@ const { setUserDefault } = require('node-mac-userdefaults')
9191
setUserDefault('boolean', 'ApplePressAndHoldEnabled', true)
9292
```
9393

94+
Note: `NSGlobalDomain` is an invalid domain name because it isn't writeable by apps.
95+
9496
### `defaults.removeUserDefault(key[, domain])`
9597

9698
* `key` String - The `NSUserDefault` to remove, e.g `AppleInterfaceStyle`.
@@ -107,6 +109,8 @@ const { removeUserDefault } = require('node-mac-userdefaults')
107109
removeUserDefault('ApplePressAndHoldEnabled')
108110
```
109111

112+
Note: `NSGlobalDomain` is an invalid domain name because it isn't writeable by apps.
113+
110114
### `defaults.isKeyManaged(key[, domain])`
111115

112116
* `key` String - The `NSUserDefault` to check, e.g `AppleInterfaceStyle`.
@@ -123,6 +127,34 @@ const managed = isKeyManaged('ApplePressAndHoldEnabled')
123127
console.log(managed) // false
124128
```
125129

130+
### `defaults.addDomain(name)`
131+
132+
* `name` String - The name of the domain to insert into the user’s domain hierarchy.
133+
134+
Inserts the specified domain name into the user’s domain hierarchy.
135+
136+
Example:
137+
```js
138+
const { addDomain } = require('node-mac-userdefaults')
139+
140+
addDomain('DomainForMyApp')
141+
```
142+
143+
### `defaults.removeDomain(name)`
144+
145+
* `name` String - The name of the domain to remove from the user’s domain hierarchy.
146+
147+
Removed the specified domain name into the user’s domain hierarchy.
148+
149+
Example:
150+
```js
151+
const { removeDomain } = require('node-mac-userdefaults')
152+
153+
removeDomain('DomainForMyApp')
154+
```
155+
156+
Note: Passing `NSGlobalDomain` is unsupported.
157+
126158
## FAQ
127159

128160
### What are domains?
@@ -131,12 +163,8 @@ The `NSUserDefaults` database consists of a hierarchy or domains. Whenever you r
131163

132164
| Domain Name | Description | State |
133165
|---|---|---|
134-
| `NSArgumentDomain` | This is the highest priority domain - it can be used to temporarily override any preference | volatile |
135-
| `Application` | This domain is where your app’s settings are stored when you invoke `setUserDefault` methods without specifying a domain | persistent |
136-
| `NSGlobalDomain` | This domain is where system-wide settings are stored | persistent |
137-
| `Languages` | This domain contains regional preferences such as month names or date formats for each locale | volatile |
138-
| `NSRegistrationDomain` | This domain serves as a fallback for any value that is not found in the Application domain | volatile |
139-
140-
You can also specify custom domains to store a set of preferences.
166+
| `NSArgumentDomain` | The domain consisting of defaults parsed from the application’s arguments | volatile |
167+
| `NSGlobalDomain` | The domain consisting of defaults meant to be seen by all applications | persistent |
168+
| `NSRegistrationDomain` | The domain consisting of a set of temporary defaults whose values can be set by the application to ensure that searches will always be successful | volatile |
141169

142-
See [this resource](https://oleb.net/blog/2014/02/nsuserdefaults-handling-default-values/) for more information.
170+
You can also specify custom domains to store a set of preferences.

index.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,28 @@ function removeUserDefault(key, domain) {
7676
return defaults.removeUserDefault.call(this, key, domain)
7777
}
7878

79+
function addDomain(name) {
80+
if (typeof name !== 'string') {
81+
throw new TypeError('domain name must be a valid string')
82+
}
83+
84+
return defaults.addDomain.call(this, name)
85+
}
86+
87+
function removeDomain(name) {
88+
if (typeof name !== 'string') {
89+
throw new TypeError('domain name must be a valid string')
90+
}
91+
92+
return defaults.removeDomain.call(this, name)
93+
}
94+
7995
module.exports = {
96+
addDomain,
8097
getAllDefaults,
8198
getUserDefault,
8299
isKeyManaged,
83100
setUserDefault,
84-
removeUserDefault
101+
removeDomain,
102+
removeUserDefault,
85103
}

src/defaults.mm

+20
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,22 @@ void RemoveUserDefault(const Napi::CallbackInfo &info) {
276276
return Napi::Boolean::New(info.Env(), managed);
277277
}
278278

279+
// Inserts the specified domain name into the user’s domain hierarchy.
280+
void AddDomain(const Napi::CallbackInfo &info) {
281+
const std::string name = (std::string)info[0].ToString();
282+
283+
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
284+
[defaults addSuiteNamed:ToNSString(name)];
285+
}
286+
287+
// Removes the specified domain name from the user’s domain hierarchy.
288+
void RemoveDomain(const Napi::CallbackInfo &info) {
289+
const std::string name = (std::string)info[0].ToString();
290+
291+
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
292+
[defaults removeSuiteNamed:ToNSString(name)];
293+
}
294+
279295
// Initializes all functions exposed to JS.
280296
Napi::Object Init(Napi::Env env, Napi::Object exports) {
281297
exports.Set(Napi::String::New(env, "isKeyManaged"),
@@ -288,6 +304,10 @@ void RemoveUserDefault(const Napi::CallbackInfo &info) {
288304
Napi::Function::New(env, SetUserDefault));
289305
exports.Set(Napi::String::New(env, "removeUserDefault"),
290306
Napi::Function::New(env, RemoveUserDefault));
307+
exports.Set(Napi::String::New(env, "addDomain"),
308+
Napi::Function::New(env, AddDomain));
309+
exports.Set(Napi::String::New(env, "removeDomain"),
310+
Napi::Function::New(env, RemoveDomain));
291311

292312
return exports;
293313
}

test/module.spec.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ const {
44
getUserDefault,
55
isKeyManaged,
66
setUserDefault,
7-
removeUserDefault
7+
removeUserDefault,
8+
addDomain,
9+
removeDomain
810
} = require('../index')
911

1012
const VALID_TYPES = [
@@ -153,4 +155,20 @@ describe('node-mac-userdefaults', () => {
153155
}).to.throw(/domain must be a valid string/)
154156
})
155157
})
158+
159+
describe('addDomain()', () => {
160+
it('should throw on a bad domain', () => {
161+
expect(() => {
162+
addDomain(1)
163+
}).to.throw(/name must be a valid string/)
164+
})
165+
})
166+
167+
describe('removeDomain()', () => {
168+
it('should throw on a bad domain', () => {
169+
expect(() => {
170+
removeDomain(1)
171+
}).to.throw(/name must be a valid string/)
172+
})
173+
})
156174
})

0 commit comments

Comments
 (0)