@vida-global/core 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -574,6 +574,13 @@ class VidaServerController {
574
574
  }
575
575
 
576
576
 
577
+ coerceString(value, { fallback = '' } = {}) {
578
+ if (typeof value !== 'string') return fallback;
579
+ const trimmed = value.trim();
580
+ return trimmed || fallback;
581
+ }
582
+
583
+
577
584
  coerceBoolean(value, { fallback } = {}) {
578
585
  if (typeof value === 'boolean') return value;
579
586
  if (typeof value !== 'string') return fallback;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vida-global/core",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Core libraries for supporting Vida development",
5
5
  "author": "",
6
6
  "license": "ISC",
@@ -156,6 +156,52 @@ describe('VidaServerController', () => {
156
156
  });
157
157
 
158
158
 
159
+ describe('#coerceString', () => {
160
+ const controller = new FooController({}, {});
161
+
162
+ it ('returns the string when passed a non-empty string', () => {
163
+ const value = TestHelpers.Faker.Text.randomString();
164
+ expect(controller.coerceString(value)).toBe(value);
165
+ });
166
+
167
+ it ('trims surrounding whitespace', () => {
168
+ const value = TestHelpers.Faker.Text.randomString();
169
+ expect(controller.coerceString(` ${value} `)).toBe(value);
170
+ });
171
+
172
+ it ('returns the fallback for an empty string', () => {
173
+ const fallback = TestHelpers.Faker.Text.randomString();
174
+ expect(controller.coerceString('', {fallback})).toBe(fallback);
175
+ });
176
+
177
+ it ('returns the fallback for a whitespace-only string', () => {
178
+ const fallback = TestHelpers.Faker.Text.randomString();
179
+ expect(controller.coerceString(' ', {fallback})).toBe(fallback);
180
+ });
181
+
182
+ it.each([
183
+ [null],
184
+ [undefined],
185
+ [123],
186
+ [true],
187
+ [false],
188
+ [{}],
189
+ [[]],
190
+ ])('returns the fallback for non-string input (%p)', (input) => {
191
+ const fallback = TestHelpers.Faker.Text.randomString();
192
+ expect(controller.coerceString(input, {fallback})).toBe(fallback);
193
+ });
194
+
195
+ it ('defaults the fallback to an empty string when no options are given', () => {
196
+ expect(controller.coerceString(null)).toBe('');
197
+ });
198
+
199
+ it ('defaults the fallback to an empty string for empty input when no options are given', () => {
200
+ expect(controller.coerceString(' ')).toBe('');
201
+ });
202
+ });
203
+
204
+
159
205
  describe('#coerceBoolean', () => {
160
206
  const controller = new FooController({}, {});
161
207