From b5c73964cc8296e4ed1be491b0eef05b827b1704 Mon Sep 17 00:00:00 2001 From: Rafael M Date: Mon, 13 Feb 2023 16:42:51 +0100 Subject: [PATCH 01/13] feat: add isEmptyString to strings package --- CHANGELOG.md | 3 +++ packages/strings/README.md | 21 +++++++++++++++++++++ packages/strings/index.js | 2 ++ test/test.js | 10 ++++++++++ utils/strings/isEmptyString.js | 2 ++ 5 files changed, 38 insertions(+) create mode 100644 utils/strings/isEmptyString.js diff --git a/CHANGELOG.md b/CHANGELOG.md index f188e46..71964f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ by [**SWR Audio Lab**](https://lab.swr.de/) ## Changelog +- 2023-02-14 - v1.1.2 + - feat: add `isEmptyString` to strings package + - 2023-02-13 - v1.1.1 - feat: add `addTrailingZeros` to numbers package - feat: add `addLeadingZero` to numbers package diff --git a/packages/strings/README.md b/packages/strings/README.md index 3ea6882..b898dc2 100644 --- a/packages/strings/README.md +++ b/packages/strings/README.md @@ -9,6 +9,7 @@ Common string, array, object encoding and getter helpers. - [`isArray` - check if a value is a proper array](#isarray---check-if-a-value-is-a-proper-array) - [`isEmptyArray` - check if a value is an empty array](#isemptyarray---check-if-a-value-is-an-empty-array) - [`isEmptyObject` - check if a value is an empty object](#isemptyobject---check-if-a-value-is-an-empty-object) + - [`isEmptyString` - check if a value is an empty string](#isemptystring---check-if-a-value-is-an-empty-string) - [`isIncluded` - check if a value (haystack) includes another value (needle)](#isincluded---check-if-a-value-haystack-includes-another-value-needle) - [`isNull` - check if a value is null](#isnull---check-if-a-value-is-null) - [`isObject` - check if a value is a proper object](#isobject---check-if-a-value-is-a-proper-object) @@ -125,6 +126,26 @@ isEmptyObject({ hello: 'world' }) // false ``` +## `isEmptyString` - check if a value is an empty string + +- `value` (required) - Value to check + +Import the library: + +```js +const { isEmptyString } = require('@swrlab/utils/packages/strings') +``` + +Then use the toolkit: + +```js +isEmptyString('') +// true + +isEmptyString('hello world') +// false +``` + ## `isIncluded` - check if a value (haystack) includes another value (needle) - `haystack` (required) - Array or value to check diff --git a/packages/strings/index.js b/packages/strings/index.js index 7938408..d33f6f3 100644 --- a/packages/strings/index.js +++ b/packages/strings/index.js @@ -4,6 +4,7 @@ const getObjectLength = require('../../utils/strings/getObjectLength') const isArray = require('../../utils/strings/isArray') const isEmptyArray = require('../../utils/strings/isEmptyArray') const isEmptyObject = require('../../utils/strings/isEmptyObject') +const isEmptyString = require('../../utils/strings/isEmptyString') const isIncluded = require('../../utils/strings/isIncluded') const isNull = require('../../utils/strings/isNull') const isObject = require('../../utils/strings/isObject') @@ -22,6 +23,7 @@ module.exports = { isArray, isEmptyArray, isEmptyObject, + isEmptyString, isIncluded, isNull, isObject, diff --git a/test/test.js b/test/test.js index f355039..1a9fbf1 100644 --- a/test/test.js +++ b/test/test.js @@ -193,6 +193,16 @@ describe('Test Strings Package', () => { }) }) + describe('Test isEmptyString', () => { + it('isEmptyString("") = true', () => { + expect(strings.isEmptyString('')).to.equal(true) + }) + + it('isEmptyString("hello world") = false', () => { + expect(strings.isEmptyString('hello world')).to.equal(false) + }) + }) + describe('Test isIncluded', () => { it('isIncluded("hello world", "hello") = true', () => { expect(strings.isIncluded('hello world', 'hello')).to.equal(true) diff --git a/utils/strings/isEmptyString.js b/utils/strings/isEmptyString.js new file mode 100644 index 0000000..7d1045f --- /dev/null +++ b/utils/strings/isEmptyString.js @@ -0,0 +1,2 @@ +// check if a variable is an empty string +module.exports = (value) => value === '' From f076cf7b58910669fe116a42eb4df1fd8dcf6627 Mon Sep 17 00:00:00 2001 From: Rafael M Date: Tue, 14 Feb 2023 11:42:07 +0100 Subject: [PATCH 02/13] feat: add normalize to numbers package --- CHANGELOG.md | 1 + packages/numbers/README.md | 22 ++++++++++++++++++++++ packages/numbers/index.js | 2 ++ test/test.js | 10 ++++++++++ utils/numbers/normalize.js | 2 ++ 5 files changed, 37 insertions(+) create mode 100644 utils/numbers/normalize.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 71964f6..e8d0220 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ by [**SWR Audio Lab**](https://lab.swr.de/) ## Changelog - 2023-02-14 - v1.1.2 + - feat: add `normalize` to numbers package - feat: add `isEmptyString` to strings package - 2023-02-13 - v1.1.1 diff --git a/packages/numbers/README.md b/packages/numbers/README.md index 1aea59f..b3c2999 100644 --- a/packages/numbers/README.md +++ b/packages/numbers/README.md @@ -11,6 +11,7 @@ Common number and math helpers. - [`getRandomInRange` - get random int between min and max](#getrandominrange---get-random-int-between-min-and-max) - [`getSum` - get sum from array of numbers](#getsum---get-sum-from-array-of-numbers) - [`isEven` - check if a value is even](#iseven---check-if-a-value-is-even) + - [`normalize` - normalize a value by given maximum](#normalize---normalize-a-value-by-given-maximum) - [`roundTo` - round float to a specified decimal place](#roundto---round-float-to-a-specified-decimal-place) - [`toReadable` - get a number in readable format](#toreadable---get-a-number-in-readable-format) @@ -180,6 +181,27 @@ isEven(1) // false ``` +## `normalize` - normalize a value by given maximum + +- `value` (required) - Value to normalize +- `max` (required) - Maximum for normalization + +Import the library: + +```js +const { normalize } = require('@swrlab/utils/packages/numbers') +``` + +Then use the toolkit: + +```js +normalize(2, 100) +// 0.02 + +normalize(80, 100) +// 0.8 +``` + ## `roundTo` - round float to a specified decimal place - `value` (required) - Float value to round diff --git a/packages/numbers/index.js b/packages/numbers/index.js index 88a69f7..4a0c2e4 100644 --- a/packages/numbers/index.js +++ b/packages/numbers/index.js @@ -6,6 +6,7 @@ const getDiff = require('../../utils/numbers/getDiff') const getRandomInRange = require('../../utils/numbers/getRandomInRange') const getSum = require('../../utils/numbers/getSum') const isEven = require('../../utils/numbers/isEven') +const normalize = require('../../utils/numbers/normalize') const roundTo = require('../../utils/numbers/roundTo') const toReadable = require('../../utils/numbers/toReadable') @@ -18,6 +19,7 @@ module.exports = { getRandomInRange, getSum, isEven, + normalize, roundTo, toReadable, } diff --git a/test/test.js b/test/test.js index 1a9fbf1..1d7fc29 100644 --- a/test/test.js +++ b/test/test.js @@ -125,6 +125,16 @@ describe('Test Numbers Package', () => { }) }) + describe('Test normalize', () => { + it('normalize(2, 100) = 0.02', () => { + expect(numbers.normalize(2, 100)).to.equal(0.02) + }) + + it('normalize(80, 100) = 0.8', () => { + expect(numbers.normalize(80, 100)).to.equal(0.8) + }) + }) + describe('Test roundTo', () => { it('roundTo(1.23456) = 1.23', () => { expect(numbers.roundTo(1.23456)).to.equal(1.23) diff --git a/utils/numbers/normalize.js b/utils/numbers/normalize.js new file mode 100644 index 0000000..0cd910a --- /dev/null +++ b/utils/numbers/normalize.js @@ -0,0 +1,2 @@ +// normalize value by division of a maximum value +module.exports = (value, maximumValue) => value / maximumValue From c2522f3aa39d79ad1e71d8460a87a88c5b5b29f6 Mon Sep 17 00:00:00 2001 From: Rafael M Date: Tue, 14 Feb 2023 12:58:13 +0100 Subject: [PATCH 03/13] feat: add getJsonKeys to helpers package --- CHANGELOG.md | 1 + packages/helpers/README.md | 18 +++++++++++ packages/helpers/index.js | 2 ++ test/test.js | 58 +++++++++++++++++++++--------------- utils/helpers/getJsonKeys.js | 10 +++++++ 5 files changed, 65 insertions(+), 24 deletions(-) create mode 100644 utils/helpers/getJsonKeys.js diff --git a/CHANGELOG.md b/CHANGELOG.md index e8d0220..0b9acd1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ by [**SWR Audio Lab**](https://lab.swr.de/) ## Changelog - 2023-02-14 - v1.1.2 + - feat: add `getJsonKeys` to helpers package - feat: add `normalize` to numbers package - feat: add `isEmptyString` to strings package diff --git a/packages/helpers/README.md b/packages/helpers/README.md index 8321300..a10faac 100644 --- a/packages/helpers/README.md +++ b/packages/helpers/README.md @@ -4,6 +4,7 @@ Common functions and helpers. - [SWR Audio Lab / Helpers](#swr-audio-lab--helpers) - [Install](#install) + - [`getJsonKeys` - get all keys of json input](#getjsonkeys---get-all-keys-of-json-input) - [`sleep` - sleep a given time (async)](#sleep---sleep-a-given-time-async) ## Install @@ -14,6 +15,23 @@ Add the parent package to your dependencies: yarn add @swrlab/utils ``` +## `getJsonKeys` - get all keys of json input + +- `value` (required) - Json to get keys from + +Import the library: + +```js +const { getJsonKeys } = require('@swrlab/utils/packages/helpers') +``` + +Then use the toolkit: + +```js +getJsonKeys({ hello: 'world', foo: 'bar' }) +// ['hello', 'foo'] +``` + ## `sleep` - sleep a given time (async) - `value` (required) - Value to sleep (in ms) diff --git a/packages/helpers/index.js b/packages/helpers/index.js index 8e78484..28294dd 100644 --- a/packages/helpers/index.js +++ b/packages/helpers/index.js @@ -1,7 +1,9 @@ // import packages +const getJsonKeys = require('../../utils/helpers/getJsonKeys') const sleep = require('../../utils/helpers/sleep') // export packages module.exports = { + getJsonKeys, sleep, } diff --git a/test/test.js b/test/test.js index 1d7fc29..0b82644 100644 --- a/test/test.js +++ b/test/test.js @@ -26,6 +26,16 @@ describe('Test ARD Package', () => { // Test Helpers Package describe('Test Helpers Package', () => { + describe('Test getJsonKeys', () => { + it("getJsonKeys({ hello: 'world', foo: 'bar' }) = ['hello', 'foo']", () => { + const test = { hello: 'world', foo: 'bar' } + const result = ['hello', 'foo'] + const testResult = helpers.getJsonKeys(test) + expect(testResult[0]).to.equal(result[0]) + expect(testResult[1]).to.equal(result[1]) + }) + }) + describe('Test sleep', () => { it('sleep(1e3) will sleep 1s', async () => { const time = 1e3 @@ -155,30 +165,30 @@ describe('Test Numbers Package', () => { // Test Strings Package describe('Test Strings Package', () => { describe('Test capitalize', () => { - it('capitalize("a") = A', () => { + it("capitalize('a') = 'A'", () => { expect(strings.capitalize('a')).to.equal('A') }) - it('capitalize("apple") = Apple', () => { + it("capitalize('apple') = 'Apple'", () => { expect(strings.capitalize('apple')).to.equal('Apple') }) }) describe('Test getObjectLength', () => { - it('getObjectLength({ hello: "world" }) = 1', () => { + it("getObjectLength({ hello: 'world' }) = 1", () => { expect(strings.getObjectLength({ hello: 'world' })).to.equal(1) }) - it('getObjectLength({ hello: "world", foo: "bar" }) = 2', () => { + it("getObjectLength({ hello: 'world', foo: 'bar' }) = 2", () => { expect(strings.getObjectLength({ hello: 'world', foo: 'bar' })).to.equal(2) }) }) describe('Test isArray', () => { - it('isArray(["hello world"]) = true', () => { + it("isArray(['hello world']) = true", () => { expect(strings.isArray(['hello world'])).to.equal(true) }) - it('isArray({ hello: "world" }) = false', () => { + it("isArray({ hello: 'world' }) = false", () => { expect(strings.isArray({ hello: 'world' })).to.equal(false) }) }) @@ -188,7 +198,7 @@ describe('Test Strings Package', () => { expect(strings.isEmptyArray([])).to.equal(true) }) - it('isEmptyArray(["hello world"]) = false', () => { + it("isEmptyArray(['hello world']) = false", () => { expect(strings.isEmptyArray(['hello world'])).to.equal(false) }) }) @@ -198,27 +208,27 @@ describe('Test Strings Package', () => { expect(strings.isEmptyObject({})).to.equal(true) }) - it('isEmptyObject({ hello: "world" }) = false', () => { + it("isEmptyObject({ hello: 'world' }) = false", () => { expect(strings.isEmptyObject({ hello: 'world' })).to.equal(false) }) }) describe('Test isEmptyString', () => { - it('isEmptyString("") = true', () => { + it("isEmptyString('') = true", () => { expect(strings.isEmptyString('')).to.equal(true) }) - it('isEmptyString("hello world") = false', () => { + it("isEmptyString('hello world') = false", () => { expect(strings.isEmptyString('hello world')).to.equal(false) }) }) describe('Test isIncluded', () => { - it('isIncluded("hello world", "hello") = true', () => { + it("isIncluded('hello world', 'hello') = true", () => { expect(strings.isIncluded('hello world', 'hello')).to.equal(true) }) - it('isIncluded("hello world", "earth") = false', () => { + it("isIncluded('hello world', 'earth') = false", () => { expect(strings.isIncluded('hello world', 'earth')).to.equal(false) }) }) @@ -234,11 +244,11 @@ describe('Test Strings Package', () => { }) describe('Test isObject', () => { - it('isObject({ hello: "world" }) = true', () => { + it("isObject({ hello: 'world' }) = true", () => { expect(strings.isObject({ hello: 'world' })).to.equal(true) }) - it('isObject("hello world") = false', () => { + it("isObject('hello world') = false", () => { expect(strings.isObject('hello world')).to.equal(false) }) }) @@ -254,7 +264,7 @@ describe('Test Strings Package', () => { }) describe('Test notEmptyArray', () => { - it('notEmptyArray(["hello world"]) = true', () => { + it("notEmptyArray(['hello world']) = true", () => { expect(strings.notEmptyArray(['hello world'])).to.equal(true) }) @@ -264,7 +274,7 @@ describe('Test Strings Package', () => { }) describe('Test notEmptyObject', () => { - it('notEmptyObject({ hello: "world" }) = true', () => { + it("notEmptyObject({ hello: 'world' }) = true", () => { expect(strings.notEmptyObject({ hello: 'world' })).to.equal(true) }) @@ -274,7 +284,7 @@ describe('Test Strings Package', () => { }) describe('Test notNullOrUndefined', () => { - it('notNullOrUndefined("hello world") = true', () => { + it("notNullOrUndefined('hello world') = true", () => { expect(strings.notNullOrUndefined('hello world')).to.equal(true) }) @@ -288,29 +298,29 @@ describe('Test Strings Package', () => { }) describe('Test pluralize', () => { - it('pluralize(1, "Apple") = 1 Apple', () => { + it("pluralize(1, 'Apple') = '1 Apple'", () => { expect(strings.pluralize(1, 'Apple')).to.equal('1 Apple') }) - it('pluralize(2, "Apple") = 2 Apples', () => { + it("pluralize(2, 'Apple') = '2 Apples'", () => { expect(strings.pluralize(2, 'Apple')).to.equal('2 Apples') }) - it('pluralize(1, "Child", "Children") = 1 Child', () => { + it("pluralize(1, 'Child', 'Children') = '1 Child'", () => { expect(strings.pluralize(1, 'Child', 'Children')).to.equal('1 Child') }) - it('pluralize(2, "Child", "Children") = 2 Children', () => { + it("pluralize(2, 'Child', 'Children') = '2 Children'", () => { expect(strings.pluralize(2, 'Child', 'Children')).to.equal('2 Children') }) }) describe('Test removeDoubleSpaces', () => { - it('removeDoubleSpaces("hello world")) = hello world', () => { + it("removeDoubleSpaces('hello world')) = 'hello world'", () => { expect(strings.removeDoubleSpaces('hello world')).to.equal('hello world') }) - it('removeDoubleSpaces("hello world once again")) = hello world once again', () => { + it("removeDoubleSpaces('hello world once again')) = 'hello world once again'", () => { expect(strings.removeDoubleSpaces('hello world once again')).to.equal( 'hello world once again' ) @@ -318,7 +328,7 @@ describe('Test Strings Package', () => { }) describe('Test toHex', () => { - it('toHex("hello world")) = 68656c6c6f20776f726c64', () => { + it("toHex('hello world')) = '68656c6c6f20776f726c64'", () => { expect(strings.toHex('hello world')).to.equal('68656c6c6f20776f726c64') }) }) diff --git a/utils/helpers/getJsonKeys.js b/utils/helpers/getJsonKeys.js new file mode 100644 index 0000000..43b1f14 --- /dev/null +++ b/utils/helpers/getJsonKeys.js @@ -0,0 +1,10 @@ +// get all keys of given json +module.exports = (json) => { + const keys = [] + for (const key in json) { + if ({}.hasOwnProperty.call(json, key)) { + keys.push(key) + } + } + return keys +} From 918095d1bdf0abe548df77d6b141fcaa04e9520f Mon Sep 17 00:00:00 2001 From: Rafael M Date: Tue, 14 Feb 2023 14:52:10 +0100 Subject: [PATCH 04/13] feat: add arrayToObjectCount to helpers package --- CHANGELOG.md | 1 + packages/helpers/README.md | 18 ++++++++++++++++++ packages/helpers/index.js | 2 ++ test/test.js | 10 ++++++++++ utils/helpers/arrayToObjectCount.js | 8 ++++++++ 5 files changed, 39 insertions(+) create mode 100644 utils/helpers/arrayToObjectCount.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b9acd1..c4e8869 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ by [**SWR Audio Lab**](https://lab.swr.de/) ## Changelog - 2023-02-14 - v1.1.2 + - feat: add `arrayToObjectCount` to helpers package - feat: add `getJsonKeys` to helpers package - feat: add `normalize` to numbers package - feat: add `isEmptyString` to strings package diff --git a/packages/helpers/README.md b/packages/helpers/README.md index a10faac..586e6b6 100644 --- a/packages/helpers/README.md +++ b/packages/helpers/README.md @@ -4,6 +4,7 @@ Common functions and helpers. - [SWR Audio Lab / Helpers](#swr-audio-lab--helpers) - [Install](#install) + - [`arrayToObjectCount` - reduce array elements to object with count](#arraytoobjectcount---reduce-array-elements-to-object-with-count) - [`getJsonKeys` - get all keys of json input](#getjsonkeys---get-all-keys-of-json-input) - [`sleep` - sleep a given time (async)](#sleep---sleep-a-given-time-async) @@ -15,6 +16,23 @@ Add the parent package to your dependencies: yarn add @swrlab/utils ``` +## `arrayToObjectCount` - reduce array elements to object with count + +- `value` (required) - Array to get entries from + +Import the library: + +```js +const { arrayToObjectCount } = require('@swrlab/utils/packages/helpers') +``` + +Then use the toolkit: + +```js +arrayToObjectCount(['foo', 'bar', 'bar']) +// { bar: 2, foo: 1 } +``` + ## `getJsonKeys` - get all keys of json input - `value` (required) - Json to get keys from diff --git a/packages/helpers/index.js b/packages/helpers/index.js index 28294dd..8efd1c2 100644 --- a/packages/helpers/index.js +++ b/packages/helpers/index.js @@ -1,9 +1,11 @@ // import packages +const arrayToObjectCount = require('../../utils/helpers/arrayToObjectCount') const getJsonKeys = require('../../utils/helpers/getJsonKeys') const sleep = require('../../utils/helpers/sleep') // export packages module.exports = { + arrayToObjectCount, getJsonKeys, sleep, } diff --git a/test/test.js b/test/test.js index 0b82644..3046f5b 100644 --- a/test/test.js +++ b/test/test.js @@ -26,6 +26,16 @@ describe('Test ARD Package', () => { // Test Helpers Package describe('Test Helpers Package', () => { + describe('Test arrayToObjectCount', () => { + it("arrayToObjectCount(['foo', 'bar', 'bar']) = { bar: 2, foo: 1 }", () => { + const test = ['foo', 'bar', 'bar'] + const result = { bar: 2, foo: 1 } + const testResult = helpers.arrayToObjectCount(test) + expect(testResult.bar).to.equal(result.bar) + expect(testResult.foo).to.equal(result.foo) + }) + }) + describe('Test getJsonKeys', () => { it("getJsonKeys({ hello: 'world', foo: 'bar' }) = ['hello', 'foo']", () => { const test = { hello: 'world', foo: 'bar' } diff --git a/utils/helpers/arrayToObjectCount.js b/utils/helpers/arrayToObjectCount.js new file mode 100644 index 0000000..fe6ef00 --- /dev/null +++ b/utils/helpers/arrayToObjectCount.js @@ -0,0 +1,8 @@ +// reduce array elements to object with count +module.exports = (array) => { + return array.reduce((obj, name) => { + // eslint-disable-next-line no-plusplus + obj[name] = obj[name] ? ++obj[name] : 1 + return obj + }, {}) +} From 96eb4f96d94527bbc153682dbd41e614abb0c8e3 Mon Sep 17 00:00:00 2001 From: Rafael M Date: Wed, 15 Feb 2023 15:18:56 +0100 Subject: [PATCH 05/13] chore: split up test into package-files --- package.json | 2 +- tests/ard.js | 21 ++++ tests/helpers.js | 45 +++++++++ tests/numbers.js | 128 +++++++++++++++++++++++++ test/test.js => tests/strings.js | 160 ------------------------------- 5 files changed, 195 insertions(+), 161 deletions(-) create mode 100644 tests/ard.js create mode 100644 tests/helpers.js create mode 100644 tests/numbers.js rename test/test.js => tests/strings.js (53%) diff --git a/package.json b/package.json index d2cd891..f3ad5bf 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "ard:publishers": "node -r dotenv/config scripts/ard/publishers.js", "ard:pub-sort": "node -r dotenv/config scripts/ard/sortPubByExtId.js", "lint": "eslint .", - "test": "mocha test/test.js -r dotenv/config", + "test": "mocha tests/**.js -r dotenv/config", "reinstall": "rm -rf node_modules && rm yarn.lock && yarn" }, "author": "SWR Audio Lab ", diff --git a/tests/ard.js b/tests/ard.js new file mode 100644 index 0000000..cad292f --- /dev/null +++ b/tests/ard.js @@ -0,0 +1,21 @@ +/* + + by SWR audio lab + simple tests with mocha and chai + +*/ + +// Add eslint exceptions for chai +/* global describe it */ + +const { expect } = require('chai') +const { createHashedId } = require('../packages/ard') + +// Test ARD Package +describe('Test ARD Package', () => { + describe('Test ARD-CoreID Hash', () => { + it("createHashedId('test') = 0c171b2e54a30c11", () => { + expect(createHashedId('test')).to.equal('0c171b2e54a30c11') + }) + }) +}) diff --git a/tests/helpers.js b/tests/helpers.js new file mode 100644 index 0000000..cba8a5d --- /dev/null +++ b/tests/helpers.js @@ -0,0 +1,45 @@ +/* + + by SWR audio lab + simple tests with mocha and chai + +*/ + +// Add eslint exceptions for chai +/* global describe it */ + +const { expect } = require('chai') +const helpers = require('../packages/helpers') + +// Test Helpers Package +describe('Test Helpers Package', () => { + describe('Test arrayToObjectCount', () => { + it("arrayToObjectCount(['foo', 'bar', 'bar']) = { bar: 2, foo: 1 }", () => { + const test = ['foo', 'bar', 'bar'] + const result = { bar: 2, foo: 1 } + const testResult = helpers.arrayToObjectCount(test) + expect(testResult.bar).to.equal(result.bar) + expect(testResult.foo).to.equal(result.foo) + }) + }) + + describe('Test getJsonKeys', () => { + it("getJsonKeys({ hello: 'world', foo: 'bar' }) = ['hello', 'foo']", () => { + const test = { hello: 'world', foo: 'bar' } + const result = ['hello', 'foo'] + const testResult = helpers.getJsonKeys(test) + expect(testResult[0]).to.equal(result[0]) + expect(testResult[1]).to.equal(result[1]) + }) + }) + + describe('Test sleep', () => { + it('sleep(1e3) will sleep 1s', async () => { + const time = 1e3 + const before = Date.now() + await helpers.sleep(time) + const after = Date.now() + expect(after - before).to.be.greaterThanOrEqual(time) + }) + }) +}) diff --git a/tests/numbers.js b/tests/numbers.js new file mode 100644 index 0000000..94cdf9b --- /dev/null +++ b/tests/numbers.js @@ -0,0 +1,128 @@ +/* eslint-disable sonarjs/no-duplicate-string */ +/* + + by SWR audio lab + simple tests with mocha and chai + +*/ + +// Add eslint exceptions for chai +/* global describe it */ + +const { expect } = require('chai') +const numbers = require('../packages/numbers') + +// Test Numbers Package +describe('Test Numbers Package', () => { + describe('Test addLeadingZero', () => { + it("addLeadingZero(1) = '01'", () => { + expect(numbers.addLeadingZero(1)).to.equal('01') + }) + + it("addLeadingZero(10) = '10'", () => { + expect(numbers.addLeadingZero(10)).to.equal('10') + }) + }) + + describe('Test addTrailingZeros', () => { + it("addTrailingZeros(1, 5) = '1.00000'", () => { + expect(numbers.addTrailingZeros(1, 5)).to.equal('1.00000') + }) + + it("addTrailingZeros(1.1, 5) = '1.10000'", () => { + expect(numbers.addTrailingZeros(1.1, 5)).to.equal('1.10000') + }) + + it("addTrailingZeros('1.2', 5) = '1.20000'", () => { + expect(numbers.addTrailingZeros('1.2', 5)).to.equal('1.20000') + }) + + it("addTrailingZeros(2, 2, ',') = '2,00'", () => { + expect(numbers.addTrailingZeros(2, 2, ',')).to.equal('2,00') + }) + + it("addTrailingZeros(2.1, 2, ',') = '2,10'", () => { + expect(numbers.addTrailingZeros(2.1, 2, ',')).to.equal('2,10') + }) + + it("addTrailingZeros('2,2', 2, ','') = '2,20'", () => { + expect(numbers.addTrailingZeros('2,2', 2, ',')).to.equal('2,20') + }) + }) + + describe('Test getAverage', () => { + it('getAverage([1, 2, 3]) = 2', () => { + expect(numbers.getAverage([1, 2, 3])).to.equal(2) + }) + + it('getAverage([1.2, 2.4, 3.6], 1) = 2.4', () => { + expect(numbers.getAverage([1.2, 2.4, 3.6], 1)).to.equal(2.4) + }) + }) + + describe('Test getDiff', () => { + it('getDiff(2, 1) = 1', () => { + expect(numbers.getDiff(2, 1)).to.equal(1) + }) + + it('getDiff(1, 2) = -1', () => { + expect(numbers.getDiff(1, 2)).to.equal(-1) + }) + }) + + describe('Test getRandomInRange', () => { + it('getRandomInRange(1, 5) = 1,2,3,4 or 5', () => { + expect([1, 2, 3, 4, 5]).to.include(numbers.getRandomInRange(1, 5)) + }) + + it('getRandomInRange(5, 9) = 5,6,7,8 or 9', () => { + expect([5, 6, 7, 8, 9]).to.include(numbers.getRandomInRange(5, 9)) + }) + }) + + describe('Test getSum', () => { + it('getSum([1, 2, 3]) = 6', () => { + expect(numbers.getSum([1, 2, 3])).to.equal(6) + }) + + it('getSum([1.2, 2.4, 3.6], 1) = 7.2', () => { + expect(numbers.getSum([1.2, 2.4, 3.6], 1)).to.equal(7.2) + }) + }) + + describe('Test isEven', () => { + it('isEven(2) = true', () => { + expect(numbers.isEven(2)).to.equal(true) + }) + + it('isEven(1) = false', () => { + expect(numbers.isEven(1)).to.equal(false) + }) + }) + + describe('Test normalize', () => { + it('normalize(2, 100) = 0.02', () => { + expect(numbers.normalize(2, 100)).to.equal(0.02) + }) + + it('normalize(80, 100) = 0.8', () => { + expect(numbers.normalize(80, 100)).to.equal(0.8) + }) + }) + + describe('Test roundTo', () => { + it('roundTo(1.23456) = 1.23', () => { + expect(numbers.roundTo(1.23456)).to.equal(1.23) + }) + + it('roundTo(1.23456, 4) = 1.2346', () => { + expect(numbers.roundTo(1.23456, 4)).to.equal(1.2346) + }) + }) + + describe('Test toReadable', () => { + it("toReadable(1234567) = '1.234.567'", () => { + expect(numbers.toReadable(1234567)).to.equal('1.234.567') + }) + }) +}) diff --git a/test/test.js b/tests/strings.js similarity index 53% rename from test/test.js rename to tests/strings.js index 3046f5b..c151321 100644 --- a/test/test.js +++ b/tests/strings.js @@ -10,168 +10,8 @@ /* global describe it */ const { expect } = require('chai') -const createHashedId = require('../utils/ard/createHashedId') -const helpers = require('../packages/helpers') -const numbers = require('../packages/numbers') const strings = require('../packages/strings') -// Test ARD Package -describe('Test ARD Package', () => { - describe('Test ARD-CoreID Hash', () => { - it('createHashedId("test") = 0c171b2e54a30c11', () => { - expect(createHashedId('test')).to.equal('0c171b2e54a30c11') - }) - }) -}) - -// Test Helpers Package -describe('Test Helpers Package', () => { - describe('Test arrayToObjectCount', () => { - it("arrayToObjectCount(['foo', 'bar', 'bar']) = { bar: 2, foo: 1 }", () => { - const test = ['foo', 'bar', 'bar'] - const result = { bar: 2, foo: 1 } - const testResult = helpers.arrayToObjectCount(test) - expect(testResult.bar).to.equal(result.bar) - expect(testResult.foo).to.equal(result.foo) - }) - }) - - describe('Test getJsonKeys', () => { - it("getJsonKeys({ hello: 'world', foo: 'bar' }) = ['hello', 'foo']", () => { - const test = { hello: 'world', foo: 'bar' } - const result = ['hello', 'foo'] - const testResult = helpers.getJsonKeys(test) - expect(testResult[0]).to.equal(result[0]) - expect(testResult[1]).to.equal(result[1]) - }) - }) - - describe('Test sleep', () => { - it('sleep(1e3) will sleep 1s', async () => { - const time = 1e3 - const before = Date.now() - await helpers.sleep(time) - const after = Date.now() - expect(after - before).to.be.greaterThanOrEqual(time) - }) - }) -}) - -// Test Numbers Package -describe('Test Numbers Package', () => { - describe('Test addLeadingZero', () => { - it("addLeadingZero(1) = '01'", () => { - expect(numbers.addLeadingZero(1)).to.equal('01') - }) - - it("addLeadingZero(10) = '10'", () => { - expect(numbers.addLeadingZero(10)).to.equal('10') - }) - }) - - describe('Test addTrailingZeros', () => { - it("addTrailingZeros(1, 5) = '1.00000'", () => { - expect(numbers.addTrailingZeros(1, 5)).to.equal('1.00000') - }) - - it("addTrailingZeros(1.1, 5) = '1.10000'", () => { - expect(numbers.addTrailingZeros(1.1, 5)).to.equal('1.10000') - }) - - it("addTrailingZeros('1.2', 5) = '1.20000'", () => { - expect(numbers.addTrailingZeros('1.2', 5)).to.equal('1.20000') - }) - - it("addTrailingZeros(2, 2, ',') = '2,00'", () => { - expect(numbers.addTrailingZeros(2, 2, ',')).to.equal('2,00') - }) - - it("addTrailingZeros(2.1, 2, ',') = '2,10'", () => { - expect(numbers.addTrailingZeros(2.1, 2, ',')).to.equal('2,10') - }) - - it("addTrailingZeros('2,2', 2, ','') = '2,20'", () => { - expect(numbers.addTrailingZeros('2,2', 2, ',')).to.equal('2,20') - }) - }) - - describe('Test getAverage', () => { - it('getAverage([1, 2, 3]) = 2', () => { - expect(numbers.getAverage([1, 2, 3])).to.equal(2) - }) - - it('getAverage([1.2, 2.4, 3.6], 1) = 2.4', () => { - expect(numbers.getAverage([1.2, 2.4, 3.6], 1)).to.equal(2.4) - }) - }) - - describe('Test getDiff', () => { - it('getDiff(2, 1) = 1', () => { - expect(numbers.getDiff(2, 1)).to.equal(1) - }) - - it('getDiff(1, 2) = -1', () => { - expect(numbers.getDiff(1, 2)).to.equal(-1) - }) - }) - - describe('Test getRandomInRange', () => { - it('getRandomInRange(1, 5) = 1,2,3,4 or 5', () => { - expect([1, 2, 3, 4, 5]).to.include(numbers.getRandomInRange(1, 5)) - }) - - it('getRandomInRange(5, 9) = 5,6,7,8 or 9', () => { - expect([5, 6, 7, 8, 9]).to.include(numbers.getRandomInRange(5, 9)) - }) - }) - - describe('Test getSum', () => { - it('getSum([1, 2, 3]) = 6', () => { - expect(numbers.getSum([1, 2, 3])).to.equal(6) - }) - - it('getSum([1.2, 2.4, 3.6], 1) = 7.2', () => { - expect(numbers.getSum([1.2, 2.4, 3.6], 1)).to.equal(7.2) - }) - }) - - describe('Test isEven', () => { - it('isEven(2) = true', () => { - expect(numbers.isEven(2)).to.equal(true) - }) - - it('isEven(1) = false', () => { - expect(numbers.isEven(1)).to.equal(false) - }) - }) - - describe('Test normalize', () => { - it('normalize(2, 100) = 0.02', () => { - expect(numbers.normalize(2, 100)).to.equal(0.02) - }) - - it('normalize(80, 100) = 0.8', () => { - expect(numbers.normalize(80, 100)).to.equal(0.8) - }) - }) - - describe('Test roundTo', () => { - it('roundTo(1.23456) = 1.23', () => { - expect(numbers.roundTo(1.23456)).to.equal(1.23) - }) - - it('roundTo(1.23456, 4) = 1.2346', () => { - expect(numbers.roundTo(1.23456, 4)).to.equal(1.2346) - }) - }) - - describe('Test toReadable', () => { - it("toReadable(1234567) = '1.234.567'", () => { - expect(numbers.toReadable(1234567)).to.equal('1.234.567') - }) - }) -}) - // Test Strings Package describe('Test Strings Package', () => { describe('Test capitalize', () => { From a1d50f329d3738a22d8f4d8f44e131bf35f65c74 Mon Sep 17 00:00:00 2001 From: Rafael M Date: Wed, 15 Feb 2023 15:49:40 +0100 Subject: [PATCH 06/13] feat: add date package with time helpers --- CHANGELOG.md | 3 +- package.json | 1 + packages/date/README.md | 140 ++++++++++++++++++++++++++++++ packages/date/index.js | 19 ++++ tests/date.js | 68 +++++++++++++++ utils/date/getDateHourMinutes.js | 5 ++ utils/date/getDayMonthYear.js | 4 + utils/date/getFullRelativeTime.js | 5 ++ utils/date/getHourMinutes.js | 4 + utils/date/getIsoRelativeTime.js | 4 + utils/date/getRelativeTime.js | 4 + utils/date/getYearMonthDay.js | 4 + yarn.lock | 5 ++ 13 files changed, 265 insertions(+), 1 deletion(-) create mode 100644 packages/date/README.md create mode 100644 packages/date/index.js create mode 100644 tests/date.js create mode 100644 utils/date/getDateHourMinutes.js create mode 100644 utils/date/getDayMonthYear.js create mode 100644 utils/date/getFullRelativeTime.js create mode 100644 utils/date/getHourMinutes.js create mode 100644 utils/date/getIsoRelativeTime.js create mode 100644 utils/date/getRelativeTime.js create mode 100644 utils/date/getYearMonthDay.js diff --git a/CHANGELOG.md b/CHANGELOG.md index c4e8869..d72d3fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,8 @@ by [**SWR Audio Lab**](https://lab.swr.de/) ## Changelog -- 2023-02-14 - v1.1.2 +- 2023-02-15 - v1.1.2 + - feat: add date package with time functions - feat: add `arrayToObjectCount` to helpers package - feat: add `getJsonKeys` to helpers package - feat: add `normalize` to numbers package diff --git a/package.json b/package.json index f3ad5bf..fcf1774 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "abort-controller": "^3.0.0", "aws-sdk": "^2.1313.0", "chai": "^4.3.7", + "luxon": "^3.2.1", "node-crc": "swrlab/node-crc#v2.1.0", "undici": "^5.19.1", "uuid": "9.0.0" diff --git a/packages/date/README.md b/packages/date/README.md new file mode 100644 index 0000000..1c605a6 --- /dev/null +++ b/packages/date/README.md @@ -0,0 +1,140 @@ +# SWR Audio Lab / Date + +Date functions and time helpers. + +- [SWR Audio Lab / Date](#swr-audio-lab--date) + - [Install](#install) + - [`getDateHourMinutes` - get date with hours and minutes](#getdatehourminutes---get-date-with-hours-and-minutes) + - [`getDayMonthYear` - get date with month and year](#getdaymonthyear---get-date-with-month-and-year) + - [`getFullRelativeTime` - get full date with relative years](#getfullrelativetime---get-full-date-with-relative-years) + - [`getHourMinutes` - get hours and minutes from iso date](#gethourminutes---get-hours-and-minutes-from-iso-date) + - [`getIsoRelativeTime` - get iso date with relative years](#getisorelativetime---get-iso-date-with-relative-years) + - [`getRelativeTime` - get relative years from iso date](#getrelativetime---get-relative-years-from-iso-date) + - [`getYearMonthDay` - get YYYYMMDD from iso date](#getyearmonthday---get-yyyymmdd-from-iso-date) + +## Install + +Add the parent package to your dependencies: + +```sh +yarn add @swrlab/utils +``` + +## `getDateHourMinutes` - get date with hours and minutes + +- `value` (required) - Date as ISO string + +Import the library: + +```js +const { getDateHourMinutes } = require('@swrlab/utils/packages/date') +``` + +Then use the toolkit: + +```js +getDateHourMinutes('2038-01-19T03:14:08.000') +// 'Di, 19. Januar 2038 - 03:14 Uhr' +``` + +## `getDayMonthYear` - get date with month and year + +- `value` (required) - Date as ISO string + +Import the library: + +```js +const { getDayMonthYear } = require('@swrlab/utils/packages/date') +``` + +Then use the toolkit: + +```js +getDayMonthYear('2038-01-19T03:14:08.000') +// 'Di, 19. Januar 2038' +``` + +## `getFullRelativeTime` - get full date with relative years + +- `value` (required) - Date as ISO string + +Import the library: + +```js +const { getFullRelativeTime } = require('@swrlab/utils/packages/date') +``` + +Then use the toolkit: + +```js +getFullRelativeTime('2038-01-19T03:14:08.000') +// 'Di, 19. Januar 2038 - 03:14 Uhr (in YY Jahren)' +``` + +## `getHourMinutes` - get hours and minutes from iso date + +- `value` (required) - Date as ISO string + +Import the library: + +```js +const { getHourMinutes } = require('@swrlab/utils/packages/date') +``` + +Then use the toolkit: + +```js +getHourMinutes('2038-01-19T03:14:08.000') +// '03:14' +``` + +## `getIsoRelativeTime` - get iso date with relative years + +- `value` (required) - Date as ISO string + +Import the library: + +```js +const { getIsoRelativeTime } = require('@swrlab/utils/packages/date') +``` + +Then use the toolkit: + +```js +getIsoRelativeTime('2038-01-19T03:14:08.000') +// '2038-01-19T03:14:08.000 (in YY Jahren)' +``` + +## `getRelativeTime` - get relative years from iso date + +- `value` (required) - Date as ISO string + +Import the library: + +```js +const { getRelativeTime } = require('@swrlab/utils/packages/date') +``` + +Then use the toolkit: + +```js +getRelativeTime('2038-01-19T03:14:08.000') +// '(in YY Jahren)' +``` + +## `getYearMonthDay` - get YYYYMMDD from iso date + +- `value` (required) - Date as ISO string + +Import the library: + +```js +const { getYearMonthDay } = require('@swrlab/utils/packages/date') +``` + +Then use the toolkit: + +```js +getYearMonthDay('2038-01-19T03:14:08.000') +// '20380119' +``` diff --git a/packages/date/index.js b/packages/date/index.js new file mode 100644 index 0000000..a04888e --- /dev/null +++ b/packages/date/index.js @@ -0,0 +1,19 @@ +// import packages +const getDateHourMinutes = require('../../utils/date/getDateHourMinutes') +const getDayMonthYear = require('../../utils/date/getDayMonthYear') +const getFullRelativeTime = require('../../utils/date/getFullRelativeTime') +const getHourMinutes = require('../../utils/date/getHourMinutes') +const getIsoRelativeTime = require('../../utils/date/getIsoRelativeTime') +const getRelativeTime = require('../../utils/date/getRelativeTime') +const getYearMonthDay = require('../../utils/date/getYearMonthDay') + +// export packages +module.exports = { + getDateHourMinutes, + getDayMonthYear, + getFullRelativeTime, + getHourMinutes, + getIsoRelativeTime, + getRelativeTime, + getYearMonthDay, +} diff --git a/tests/date.js b/tests/date.js new file mode 100644 index 0000000..a2d1a78 --- /dev/null +++ b/tests/date.js @@ -0,0 +1,68 @@ +/* + + by SWR audio lab + simple tests with mocha and chai + +*/ + +// Add eslint exceptions for chai +/* global describe it */ + +const { expect } = require('chai') +const date = require('../packages/date') + +const testDate = '2038-01-19T03:14:08.000' +const relativeTime = 2147483647000 - new Date().getTime() +const relativeYears = parseInt(relativeTime / (1000 * 60 * 60 * 24 * 365), 10) + +// Test DateTime Package +describe('Test DateTime Package', () => { + describe('Test getDateHourMinutes', () => { + const testResult = 'Di, 19. Januar 2038 - 03:14 Uhr' + it(`getDateHourMinutes('${testDate}') = '${testResult}'`, () => { + expect(date.getDateHourMinutes(testDate)).to.equal(testResult) + }) + }) + + describe('Test getDayMonthYear', () => { + const testResult = 'Di, 19. Januar 2038' + it(`getDayMonthYear('${testDate}') = '${testResult}'`, () => { + expect(date.getDayMonthYear(testDate)).to.equal(testResult) + }) + }) + + describe('Test getFullRelativeTime', () => { + const testResult = `Di, 19. Januar 2038 - 03:14 Uhr (in ${relativeYears} Jahren)` + it(`getFullRelativeTime('${testDate}') = '${testResult}'`, () => { + expect(date.getFullRelativeTime(testDate)).to.equal(testResult) + }) + }) + + describe('Test getHourMinutes', () => { + const testResult = '03:14' + it(`getHourMinutes('${testDate}') = '${testResult}'`, () => { + expect(date.getHourMinutes(testDate)).to.equal(testResult) + }) + }) + + describe('Test getIsoRelativeTime', () => { + const testResult = `${testDate} (in ${relativeYears} Jahren)` + it(`getIsoRelativeTime('${testDate}') = '${testResult}'`, () => { + expect(date.getIsoRelativeTime(testDate)).to.equal(testResult) + }) + }) + + describe('Test getRelativeTime', () => { + const testResult = `in ${relativeYears} Jahren` + it(`getRelativeTime('${testDate}') = '${testResult}'`, () => { + expect(date.getRelativeTime(testDate)).to.equal(testResult) + }) + }) + + describe('Test getYearMonthDay', () => { + const testResult = '20380119' + it(`getYearMonthDay('${testDate}') = '${testResult}'`, () => { + expect(date.getYearMonthDay(testDate)).to.equal(testResult) + }) + }) +}) diff --git a/utils/date/getDateHourMinutes.js b/utils/date/getDateHourMinutes.js new file mode 100644 index 0000000..fcc9704 --- /dev/null +++ b/utils/date/getDateHourMinutes.js @@ -0,0 +1,5 @@ +const { DateTime } = require('luxon') + +// returns 'Di, 19. Januar 2038 - 03:14 Uhr' +const dayHourMinutes = 'ccc, d. LLLL yyyy - HH:mm' +module.exports = (date) => `${DateTime.fromISO(date).setLocale('de').toFormat(dayHourMinutes)} Uhr` diff --git a/utils/date/getDayMonthYear.js b/utils/date/getDayMonthYear.js new file mode 100644 index 0000000..cc387c8 --- /dev/null +++ b/utils/date/getDayMonthYear.js @@ -0,0 +1,4 @@ +const { DateTime } = require('luxon') + +// returns 'Do, 1. Januar 1970' +module.exports = (date) => DateTime.fromISO(date).setLocale('de').toFormat('ccc, d. LLLL yyyy') diff --git a/utils/date/getFullRelativeTime.js b/utils/date/getFullRelativeTime.js new file mode 100644 index 0000000..edcab2d --- /dev/null +++ b/utils/date/getFullRelativeTime.js @@ -0,0 +1,5 @@ +const getDateHourMinutes = require('./getDateHourMinutes') +const getRelativeTime = require('./getRelativeTime') + +// returns 'Do, 1. Januar 1970 - 00:00 Uhr (in YY Jahren)' +module.exports = (date) => `${getDateHourMinutes(date)} (${getRelativeTime(date)})` diff --git a/utils/date/getHourMinutes.js b/utils/date/getHourMinutes.js new file mode 100644 index 0000000..5a3fa3f --- /dev/null +++ b/utils/date/getHourMinutes.js @@ -0,0 +1,4 @@ +const { DateTime } = require('luxon') + +// get hours and minutes (returns '12:34') +module.exports = (date) => DateTime.fromISO(date).toFormat('HH:mm') diff --git a/utils/date/getIsoRelativeTime.js b/utils/date/getIsoRelativeTime.js new file mode 100644 index 0000000..57a8656 --- /dev/null +++ b/utils/date/getIsoRelativeTime.js @@ -0,0 +1,4 @@ +const getRelativeTime = require('./getRelativeTime') + +// get iso date with relative years +module.exports = (date) => `${date} (${getRelativeTime(date)})` diff --git a/utils/date/getRelativeTime.js b/utils/date/getRelativeTime.js new file mode 100644 index 0000000..0df4aa8 --- /dev/null +++ b/utils/date/getRelativeTime.js @@ -0,0 +1,4 @@ +const { DateTime } = require('luxon') + +// get relative years (returns 'in YY Jahren') +module.exports = (date) => DateTime.fromISO(date).setLocale('de').toRelative() diff --git a/utils/date/getYearMonthDay.js b/utils/date/getYearMonthDay.js new file mode 100644 index 0000000..1b778a8 --- /dev/null +++ b/utils/date/getYearMonthDay.js @@ -0,0 +1,4 @@ +const { DateTime } = require('luxon') + +// get YYYYMMDD (returns '19700101') +module.exports = (date) => DateTime.fromISO(date).setLocale('de').toFormat('yyyyLLdd') diff --git a/yarn.lock b/yarn.lock index 8419f2e..9a36ebf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1644,6 +1644,11 @@ lru-cache@^6.0.0: dependencies: yallist "^4.0.0" +luxon@^3.2.1: + version "3.2.1" + resolved "https://registry.yarnpkg.com/luxon/-/luxon-3.2.1.tgz#14f1af209188ad61212578ea7e3d518d18cee45f" + integrity sha512-QrwPArQCNLAKGO/C+ZIilgIuDnEnKx5QYODdDtbFaxzsbZcc/a7WFq7MhsVYgRlwawLtvOUESTlfJ+hc/USqPg== + mime-db@1.52.0, "mime-db@>= 1.43.0 < 2": version "1.52.0" resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70" From e47622ca300a8fa725e85d9a03101a330922eda5 Mon Sep 17 00:00:00 2001 From: Rafael M Date: Wed, 15 Feb 2023 17:39:39 +0100 Subject: [PATCH 07/13] chore: update node-dependencies --- package.json | 4 ++-- yarn.lock | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index fcf1774..9eceb6f 100644 --- a/package.json +++ b/package.json @@ -23,9 +23,9 @@ "license": "MIT", "private": false, "dependencies": { - "@google-cloud/storage": "^6.9.2", + "@google-cloud/storage": "^6.9.3", "abort-controller": "^3.0.0", - "aws-sdk": "^2.1313.0", + "aws-sdk": "^2.1315.0", "chai": "^4.3.7", "luxon": "^3.2.1", "node-crc": "swrlab/node-crc#v2.1.0", diff --git a/yarn.lock b/yarn.lock index 9a36ebf..54d7d37 100644 --- a/yarn.lock +++ b/yarn.lock @@ -56,10 +56,10 @@ resolved "https://registry.yarnpkg.com/@google-cloud/promisify/-/promisify-3.0.1.tgz#8d724fb280f47d1ff99953aee0c1669b25238c2e" integrity sha512-z1CjRjtQyBOYL+5Qr9DdYIfrdLBe746jRTYfaYU6MeXkqp7UfYs/jX16lFFVzZ7PGEJvqZNqYUEtb1mvDww4pA== -"@google-cloud/storage@^6.9.2": - version "6.9.2" - resolved "https://registry.yarnpkg.com/@google-cloud/storage/-/storage-6.9.2.tgz#f620c22aa817d3459035adeb23b89342ece60dc3" - integrity sha512-TIQ6G+QzHb/hR/7AIH4OU+z8fXqCLM7JiqO+nGxw61os4YxCWPD2ajW+pzz7VY5k2VtqzhoMFA6rjIfw39wJmQ== +"@google-cloud/storage@^6.9.3": + version "6.9.3" + resolved "https://registry.yarnpkg.com/@google-cloud/storage/-/storage-6.9.3.tgz#43fa4c9e30785313f68d01e6fde9bdba6d10a695" + integrity sha512-ucbHoDvjXlcR/DrJNQlCFnQSaO7pXHTPGs3Gt2TQtPQ+b7Y6DR0ztIt/CEeH+O03I41g9e+T2N1SOOVq5UyaKQ== dependencies: "@google-cloud/paginator" "^3.0.7" "@google-cloud/projectify" "^3.0.0" @@ -283,10 +283,10 @@ available-typed-arrays@^1.0.5: resolved "https://registry.yarnpkg.com/available-typed-arrays/-/available-typed-arrays-1.0.5.tgz#92f95616501069d07d10edb2fc37d3e1c65123b7" integrity sha512-DMD0KiN46eipeziST1LPP/STfDU0sufISXmjSgvVsoU2tqxctQeASejWcfNtxYKqETM1UxQ8sp2OrSBWpHY6sw== -aws-sdk@^2.1313.0: - version "2.1313.0" - resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.1313.0.tgz#e6dad914670fb3f4d5cd95c58413fca5ea5c6c86" - integrity sha512-8GMdtV2Uch3HL2c6+P3lNZFTcg/fqq9L3EWYRLb6ljCZvWKTssjdkjSJFDyTReNgeiKV224YRPYQbKpOEz4flQ== +aws-sdk@^2.1315.0: + version "2.1315.0" + resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.1315.0.tgz#24e485c21ea443b0bbc02c219601737ec2e5e1bb" + integrity sha512-fDlK3iqA0bpXW6azVBAB8RTKhJ5YUjfe6sqKQF1lKT2cYT5qU4rtH5qUlMixlWkb8oKeSMA2voW3wZZtSpBFKQ== dependencies: buffer "4.9.2" events "1.1.1" From 82c4ac1455446013acd4b54985d46f1dc70deb93 Mon Sep 17 00:00:00 2001 From: Rafael M Date: Wed, 15 Feb 2023 17:39:56 +0100 Subject: [PATCH 08/13] fix: install command in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f35dfc2..daf12ce 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ const undici = require('@swrlab/utils/packages/undici') Scripts are meant to be run locally, therefore clone the repository and first install dependencies. We prefer `yarn` for this: ```sh -yarn install +yarn install @swrlab/utils ``` Then run the desired script. From ff55cd85f85885a50ebf3211e90b8c50185b24b9 Mon Sep 17 00:00:00 2001 From: Rafael M Date: Wed, 15 Feb 2023 17:40:39 +0100 Subject: [PATCH 09/13] fix: add/use index.js as main file --- index.js | 24 ++++++++++++++++++++++++ package.json | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..d388bbb --- /dev/null +++ b/index.js @@ -0,0 +1,24 @@ +/* + + node-utils by SWR audio lab + frequently used node-packages and helpers + +*/ + +// import packages +const ard = require('./packages/ard') +const helpers = require('./packages/helpers') +const numbers = require('./packages/numbers') +const storage = require('./packages/storage-wrapper') +const strings = require('./packages/strings') +const undici = require('./packages/undici') + +// export packages +module.exports = { + ard, + helpers, + numbers, + storage, + strings, + undici, +} diff --git a/package.json b/package.json index 9eceb6f..944b8a4 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@swrlab/utils", "version": "1.1.1", "description": "Wrapping common SWR Audio Lab utils", - "main": "./src/index.js", + "main": "./index.js", "engines": { "node": ">=16" }, From 506a5055ef370529493ac5fd585b5353fe3429c2 Mon Sep 17 00:00:00 2001 From: Rafael M Date: Wed, 15 Feb 2023 17:40:49 +0100 Subject: [PATCH 10/13] chore: update package-version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 944b8a4..3703ff1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@swrlab/utils", - "version": "1.1.1", + "version": "1.1.2", "description": "Wrapping common SWR Audio Lab utils", "main": "./index.js", "engines": { From 21fd908fee12a590a64e1c9c5e1d68614acdf666 Mon Sep 17 00:00:00 2001 From: Rafael M Date: Wed, 15 Feb 2023 17:56:53 +0100 Subject: [PATCH 11/13] feat: add revYearMonthDay to date package --- CHANGELOG.md | 3 ++- packages/date/README.md | 18 ++++++++++++++++++ packages/date/index.js | 2 ++ tests/date.js | 8 ++++++++ utils/date/revYearMonthDay.js | 7 +++++++ 5 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 utils/date/revYearMonthDay.js diff --git a/CHANGELOG.md b/CHANGELOG.md index d72d3fb..856bc9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,8 @@ by [**SWR Audio Lab**](https://lab.swr.de/) ## Changelog - 2023-02-15 - v1.1.2 - - feat: add date package with time functions + - feat: add `revYearMonthDay` to date package + - feat: add `date` package with iso time functions - feat: add `arrayToObjectCount` to helpers package - feat: add `getJsonKeys` to helpers package - feat: add `normalize` to numbers package diff --git a/packages/date/README.md b/packages/date/README.md index 1c605a6..f85a0b6 100644 --- a/packages/date/README.md +++ b/packages/date/README.md @@ -11,6 +11,7 @@ Date functions and time helpers. - [`getIsoRelativeTime` - get iso date with relative years](#getisorelativetime---get-iso-date-with-relative-years) - [`getRelativeTime` - get relative years from iso date](#getrelativetime---get-relative-years-from-iso-date) - [`getYearMonthDay` - get YYYYMMDD from iso date](#getyearmonthday---get-yyyymmdd-from-iso-date) + - [`revYearMonthDay` - get DDMMYYYY from YYYYMMDD](#revyearmonthday---get-ddmmyyyy-from-yyyymmdd) ## Install @@ -138,3 +139,20 @@ Then use the toolkit: getYearMonthDay('2038-01-19T03:14:08.000') // '20380119' ``` + +## `revYearMonthDay` - get DDMMYYYY from YYYYMMDD + +- `value` (required) - Date as `YYYYMMDD` string + +Import the library: + +```js +const { revYearMonthDay } = require('@swrlab/utils/packages/date') +``` + +Then use the toolkit: + +```js +revYearMonthDay('20380119') +// '19012038' +``` diff --git a/packages/date/index.js b/packages/date/index.js index a04888e..4ab424e 100644 --- a/packages/date/index.js +++ b/packages/date/index.js @@ -6,6 +6,7 @@ const getHourMinutes = require('../../utils/date/getHourMinutes') const getIsoRelativeTime = require('../../utils/date/getIsoRelativeTime') const getRelativeTime = require('../../utils/date/getRelativeTime') const getYearMonthDay = require('../../utils/date/getYearMonthDay') +const revYearMonthDay = require('../../utils/date/revYearMonthDay') // export packages module.exports = { @@ -16,4 +17,5 @@ module.exports = { getIsoRelativeTime, getRelativeTime, getYearMonthDay, + revYearMonthDay, } diff --git a/tests/date.js b/tests/date.js index a2d1a78..9a63e23 100644 --- a/tests/date.js +++ b/tests/date.js @@ -65,4 +65,12 @@ describe('Test DateTime Package', () => { expect(date.getYearMonthDay(testDate)).to.equal(testResult) }) }) + + describe('Test revYearMonthDay', () => { + const test = '20380119' + const testResult = '19012038' + it(`revYearMonthDay('${test}') = '${testResult}'`, () => { + expect(date.revYearMonthDay(test)).to.equal(testResult) + }) + }) }) diff --git a/utils/date/revYearMonthDay.js b/utils/date/revYearMonthDay.js new file mode 100644 index 0000000..b80e671 --- /dev/null +++ b/utils/date/revYearMonthDay.js @@ -0,0 +1,7 @@ +// get DDMMYYYY from YYYYMMDD +module.exports = (date) => { + const year = date.substring(0, 4) + const month = date.substring(4, 6) + const day = date.substring(6, 8) + return `${day}${month}${year}` +} From cee8b038e9b088d6abb9d84c68f1ee9af1970623 Mon Sep 17 00:00:00 2001 From: Rafael M Date: Mon, 20 Feb 2023 11:40:51 +0100 Subject: [PATCH 12/13] fix: case sensitivity of audio-lab in file headers --- tests/ard.js | 4 ++-- tests/date.js | 4 ++-- tests/helpers.js | 4 ++-- tests/numbers.js | 4 ++-- tests/strings.js | 4 ++-- utils/ard/createHashedId.js | 2 +- utils/undici/index.js | 2 +- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/ard.js b/tests/ard.js index cad292f..921e5ac 100644 --- a/tests/ard.js +++ b/tests/ard.js @@ -1,7 +1,7 @@ /* - by SWR audio lab - simple tests with mocha and chai + by SWR Audio Lab + tests with mocha and chai */ diff --git a/tests/date.js b/tests/date.js index 9a63e23..b31ca8c 100644 --- a/tests/date.js +++ b/tests/date.js @@ -1,7 +1,7 @@ /* - by SWR audio lab - simple tests with mocha and chai + by SWR Audio Lab + tests with mocha and chai */ diff --git a/tests/helpers.js b/tests/helpers.js index cba8a5d..fb5b436 100644 --- a/tests/helpers.js +++ b/tests/helpers.js @@ -1,7 +1,7 @@ /* - by SWR audio lab - simple tests with mocha and chai + by SWR Audio Lab + tests with mocha and chai */ diff --git a/tests/numbers.js b/tests/numbers.js index 94cdf9b..518fcc2 100644 --- a/tests/numbers.js +++ b/tests/numbers.js @@ -1,8 +1,8 @@ /* eslint-disable sonarjs/no-duplicate-string */ /* - by SWR audio lab - simple tests with mocha and chai + by SWR Audio Lab + tests with mocha and chai */ diff --git a/tests/strings.js b/tests/strings.js index c151321..8616ba8 100644 --- a/tests/strings.js +++ b/tests/strings.js @@ -1,8 +1,8 @@ /* eslint-disable sonarjs/no-duplicate-string */ /* - by SWR audio lab - simple tests with mocha and chai + by SWR Audio Lab + tests with mocha and chai */ diff --git a/utils/ard/createHashedId.js b/utils/ard/createHashedId.js index 35bb79c..ae2b1a2 100644 --- a/utils/ard/createHashedId.js +++ b/utils/ard/createHashedId.js @@ -1,6 +1,6 @@ /* - by SWR audio lab + by SWR Audio Lab this file creates a CRC64-ECMA182-compliant hash based on an utf-8 encoded input string diff --git a/utils/undici/index.js b/utils/undici/index.js index d69334c..e424968 100644 --- a/utils/undici/index.js +++ b/utils/undici/index.js @@ -1,6 +1,6 @@ /* - SWR Audio Lab + by SWR Audio Lab this file runs the undici-wrapper util From f6de3577d7510465df0ad9d8ecf29cdbb39d8d07 Mon Sep 17 00:00:00 2001 From: Rafael M Date: Mon, 20 Feb 2023 11:52:20 +0100 Subject: [PATCH 13/13] chore: update node-dependencies --- package.json | 4 ++-- yarn.lock | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 3703ff1..122ad93 100644 --- a/package.json +++ b/package.json @@ -25,11 +25,11 @@ "dependencies": { "@google-cloud/storage": "^6.9.3", "abort-controller": "^3.0.0", - "aws-sdk": "^2.1315.0", + "aws-sdk": "^2.1318.0", "chai": "^4.3.7", "luxon": "^3.2.1", "node-crc": "swrlab/node-crc#v2.1.0", - "undici": "^5.19.1", + "undici": "^5.20.0", "uuid": "9.0.0" }, "devDependencies": { diff --git a/yarn.lock b/yarn.lock index 54d7d37..2021b10 100644 --- a/yarn.lock +++ b/yarn.lock @@ -283,10 +283,10 @@ available-typed-arrays@^1.0.5: resolved "https://registry.yarnpkg.com/available-typed-arrays/-/available-typed-arrays-1.0.5.tgz#92f95616501069d07d10edb2fc37d3e1c65123b7" integrity sha512-DMD0KiN46eipeziST1LPP/STfDU0sufISXmjSgvVsoU2tqxctQeASejWcfNtxYKqETM1UxQ8sp2OrSBWpHY6sw== -aws-sdk@^2.1315.0: - version "2.1315.0" - resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.1315.0.tgz#24e485c21ea443b0bbc02c219601737ec2e5e1bb" - integrity sha512-fDlK3iqA0bpXW6azVBAB8RTKhJ5YUjfe6sqKQF1lKT2cYT5qU4rtH5qUlMixlWkb8oKeSMA2voW3wZZtSpBFKQ== +aws-sdk@^2.1318.0: + version "2.1318.0" + resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.1318.0.tgz#a633c82a51d0572805f744b51ca04a3babf6c909" + integrity sha512-xRCKqx4XWXUIpjDCVHmdOSINEVCIC5+yhmgUGR9A6VfxfPs59HbxKyd5LB+CmXhVbwVUM4SRWG5O+agQj+w7Eg== dependencies: buffer "4.9.2" events "1.1.1" @@ -2369,10 +2369,10 @@ unbox-primitive@^1.0.2: has-symbols "^1.0.3" which-boxed-primitive "^1.0.2" -undici@^5.19.1: - version "5.19.1" - resolved "https://registry.yarnpkg.com/undici/-/undici-5.19.1.tgz#92b1fd3ab2c089b5a6bd3e579dcda8f1934ebf6d" - integrity sha512-YiZ61LPIgY73E7syxCDxxa3LV2yl3sN8spnIuTct60boiiRaE1J8mNWHO8Im2Zi/sFrPusjLlmRPrsyraSqX6A== +undici@^5.20.0: + version "5.20.0" + resolved "https://registry.yarnpkg.com/undici/-/undici-5.20.0.tgz#6327462f5ce1d3646bcdac99da7317f455bcc263" + integrity sha512-J3j60dYzuo6Eevbawwp1sdg16k5Tf768bxYK4TUJRH7cBM4kFCbf3mOnM/0E3vQYXvpxITbbWmBafaDbxLDz3g== dependencies: busboy "^1.6.0"