@8ms/helpers 1.0.22 → 1.0.23

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.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var index_1 = require("../index");
4
+ test('contains - does contain', function () {
5
+ var haystack = [1, 2, 3, 4];
6
+ expect((0, index_1.contains)({ haystack: haystack, needle: 1 }))
7
+ .toBeTruthy();
8
+ expect((0, index_1.contains)({ haystack: haystack, needle: 2 }))
9
+ .toBeTruthy();
10
+ expect((0, index_1.contains)({ haystack: haystack, needle: 3 }))
11
+ .toBeTruthy();
12
+ expect((0, index_1.contains)({ haystack: haystack, needle: 4 }))
13
+ .toBeTruthy();
14
+ });
15
+ test('contains - does not contain', function () {
16
+ expect((0, index_1.contains)({ haystack: [1, 2, 3, 4], needle: 9 }))
17
+ .toBeFalsy();
18
+ });
@@ -47,7 +47,7 @@ var deleteDirectory = function (_a) {
47
47
  return __generator(this, function (_b) {
48
48
  switch (_b.label) {
49
49
  case 0:
50
- cleanFolder = (0, string_1.getCleanFolder)(folder);
50
+ cleanFolder = (0, string_1.getCleanFolder)({ input: folder });
51
51
  ListObjectsV2Command = S3Lib.ListObjectsV2Command, DeleteObjectsCommand = S3Lib.DeleteObjectsCommand;
52
52
  return [4 /*yield*/, client.send(new ListObjectsV2Command({
53
53
  Bucket: bucket,
@@ -47,7 +47,7 @@ var listFiles = function (_a) {
47
47
  return __generator(this, function (_c) {
48
48
  switch (_c.label) {
49
49
  case 0:
50
- cleanFolder = (0, string_1.getCleanFolder)(folder);
50
+ cleanFolder = (0, string_1.getCleanFolder)({ input: folder });
51
51
  response = [];
52
52
  valid = [];
53
53
  ListObjectsV2Command = S3Lib.ListObjectsV2Command;
@@ -47,7 +47,7 @@ var listFolders = function (_a) {
47
47
  return __generator(this, function (_c) {
48
48
  switch (_c.label) {
49
49
  case 0:
50
- cleanFolder = (0, string_1.getCleanFolder)(folder);
50
+ cleanFolder = (0, string_1.getCleanFolder)({ input: folder });
51
51
  response = [];
52
52
  ListObjectsV2Command = S3Lib.ListObjectsV2Command;
53
53
  return [4 /*yield*/, client.send(new ListObjectsV2Command({
@@ -97,7 +97,7 @@ var readFile = function (_a) {
97
97
  response.modified.vsMidnight.minutes = (0, getMinutes_1.default)(now);
98
98
  // Convert stream into a string
99
99
  _b = response;
100
- return [4 /*yield*/, (0, string_1.getStringFromStream)(apiResponse.Body)];
100
+ return [4 /*yield*/, (0, string_1.getStringFromStream)({ stream: apiResponse.Body })];
101
101
  case 2:
102
102
  // Convert stream into a string
103
103
  _b.body = _c.sent();
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Take an input and return a number.
3
3
  */
4
- declare const getNumber: ({ defaultValue, input }?: {
4
+ declare const getNumber: ({ defaultValue, input }: {
5
5
  defaultValue?: number;
6
6
  input: any;
7
7
  }) => number;
@@ -5,15 +5,26 @@ var json_1 = require("../json");
5
5
  * Take an input and return a number.
6
6
  */
7
7
  var getNumber = function (_a) {
8
- var _b = _a === void 0 ? { defaultValue: 0, input: '' } : _a, defaultValue = _b.defaultValue, input = _b.input;
8
+ var _b = _a.defaultValue, defaultValue = _b === void 0 ? 0 : _b, _c = _a.input, input = _c === void 0 ? '' : _c;
9
9
  var actualDefaultValue = defaultValue || 0;
10
- var response;
10
+ var response = input;
11
11
  // JSON - Decode the value
12
12
  if ((0, json_1.isJson)({ input: input })) {
13
- response = JSON.parse(input);
13
+ // Only parse this if it's a string
14
+ if ('string' === typeof input) {
15
+ response = JSON.parse(input);
16
+ }
17
+ // Already decoded
18
+ else {
19
+ response = input;
20
+ }
21
+ }
22
+ // Already a number
23
+ if ('number' === typeof response) {
24
+ response = response;
14
25
  }
15
26
  // String input
16
- if ('string' === typeof response) {
27
+ else if ('string' === typeof response) {
17
28
  // Attempt to convert to number
18
29
  response = Number(response);
19
30
  // Not a number, try extracting the number value
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var index_1 = require("../index");
4
+ test('getNumber - should be number', function () {
5
+ expect((0, index_1.getNumber)({ defaultValue: 10, input: 99 }))
6
+ .toBe(99);
7
+ expect((0, index_1.getNumber)({ defaultValue: 10, input: '99' }))
8
+ .toBe(99);
9
+ expect((0, index_1.getNumber)({ defaultValue: 10, input: 5 * 25 }))
10
+ .toBe(125);
11
+ expect((0, index_1.getNumber)({ defaultValue: 10, input: '_50' }))
12
+ .toBe(50);
13
+ expect((0, index_1.getNumber)({ defaultValue: 10, input: '33.3232%' }))
14
+ .toBe(33.3232);
15
+ });
16
+ test('getNumber - revert to default', function () {
17
+ expect((0, index_1.getNumber)({ defaultValue: 10, input: {} }))
18
+ .toBe(10);
19
+ expect((0, index_1.getNumber)({ input: {} }))
20
+ .toBe(0);
21
+ });
@@ -1,5 +1,7 @@
1
1
  /**
2
2
  * Take an input that may have a / before or after the input and return just the folder.
3
3
  */
4
- declare const getCleanFolder: (input: string) => string;
4
+ declare const getCleanFolder: ({ input }: {
5
+ input: string;
6
+ }) => string;
5
7
  export default getCleanFolder;
@@ -7,6 +7,9 @@ var trim_1 = __importDefault(require("lodash/trim"));
7
7
  /**
8
8
  * Take an input that may have a / before or after the input and return just the folder.
9
9
  */
10
- var getCleanFolder = function (input) { return (0, trim_1.default)(input, '/')
11
- .trim(); };
10
+ var getCleanFolder = function (_a) {
11
+ var input = _a.input;
12
+ return (0, trim_1.default)(input, '/')
13
+ .trim();
14
+ };
12
15
  exports.default = getCleanFolder;
@@ -2,5 +2,7 @@
2
2
  * Converts a stream into a string.
3
3
  * https://stackoverflow.com/a/49428486
4
4
  */
5
- declare const getStringFromStream: (stream: any) => Promise<string>;
5
+ declare const getStringFromStream: ({ stream }: {
6
+ stream: any;
7
+ }) => Promise<string>;
6
8
  export default getStringFromStream;
@@ -4,7 +4,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
4
4
  * Converts a stream into a string.
5
5
  * https://stackoverflow.com/a/49428486
6
6
  */
7
- var getStringFromStream = function (stream) {
7
+ var getStringFromStream = function (_a) {
8
+ var stream = _a.stream;
8
9
  var chunks = [];
9
10
  return new Promise(function (resolve, reject) {
10
11
  stream.on('data', function (chunk) { return chunks.push(Buffer.from(chunk)); });
@@ -4,6 +4,6 @@ var index_1 = require("../index");
4
4
  test('getCleanFolder', function () {
5
5
  var input = ' /some-folder/ ';
6
6
  var expected = '/some-folder/';
7
- expect((0, index_1.getCleanFolder)(input))
7
+ expect((0, index_1.getCleanFolder)({ input: input }))
8
8
  .toBe(expected);
9
9
  });
@@ -1,3 +1,6 @@
1
+ /**
2
+ * Function to default to a value, whilst filtering through an instance
3
+ */
1
4
  declare const defaultTo: ({ defaultValue, instance, keys }: {
2
5
  defaultValue: any;
3
6
  instance: any;
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ var array_1 = require("../array");
3
4
  /**
4
5
  * Function to default to a value, whilst filtering through an instance
5
6
  */
6
- var array_1 = require("../array");
7
7
  var defaultTo = function (_a) {
8
8
  var defaultValue = _a.defaultValue, instance = _a.instance, keys = _a.keys;
9
9
  var keysArray = (0, array_1.getArray)({ input: keys });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var index_1 = require("../index");
4
+ test('defaultTo - exists', function () {
5
+ var instance = {
6
+ 'something': 'a b c',
7
+ 'level one': {
8
+ 'level two': 'inner',
9
+ },
10
+ };
11
+ expect((0, index_1.defaultTo)({ defaultValue: 'not found', instance: instance, keys: 'something' }))
12
+ .toBe('a b c');
13
+ expect((0, index_1.defaultTo)({ defaultValue: 'not found', instance: instance, keys: ['level one', 'level two'] }))
14
+ .toBe('inner');
15
+ });
16
+ test('defaultTo - doesnt exist', function () {
17
+ var instance = {
18
+ 'something': 'a b c',
19
+ 'level one': {
20
+ 'level two': 'inner',
21
+ },
22
+ };
23
+ expect((0, index_1.defaultTo)({ defaultValue: true, instance: instance, keys: 'not here' }))
24
+ .toBeTruthy();
25
+ expect((0, index_1.defaultTo)({ defaultValue: 'not found', instance: instance, keys: ['level one', 'level two', 'level three'] }))
26
+ .toBe('not found');
27
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@8ms/helpers",
3
- "version": "1.0.22",
3
+ "version": "1.0.23",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/8millionstories-organisation/8ms-helpers-ts.git"