Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

common Add encode, decode and uuid helpers #791

Merged
merged 8 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/asana/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-asana

## 4.0.4

### Patch Changes

- Updated dependencies [03a1a74]
- @openfn/language-common@2.1.0

## 4.0.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/asana/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-asana",
"version": "4.0.3",
"version": "4.0.4",
"description": "An adaptor to access objects in Asana",
"homepage": "https://docs.openfn.org",
"repository": {
Expand Down
7 changes: 7 additions & 0 deletions packages/azure-storage/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-azure-storage

## 2.0.4

### Patch Changes

- Updated dependencies [03a1a74]
- @openfn/language-common@2.1.0

## 2.0.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/azure-storage/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-azure-storage",
"version": "2.0.3",
"version": "2.0.4",
"description": "OpenFn adaptor for Azure Storage",
"type": "module",
"exports": {
Expand Down
7 changes: 7 additions & 0 deletions packages/beyonic/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-beyonic

## 0.3.6

### Patch Changes

- Updated dependencies [03a1a74]
- @openfn/language-common@2.1.0

## 0.3.5

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/beyonic/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-beyonic",
"version": "0.3.5",
"version": "0.3.6",
"description": "beyonic Language Pack for OpenFn",
"homepage": "https://docs.openfn.org",
"repository": {
Expand Down
7 changes: 7 additions & 0 deletions packages/bigquery/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-bigquery

## 3.0.5

### Patch Changes

- Updated dependencies [03a1a74]
- @openfn/language-common@2.1.0

## 3.0.4

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/bigquery/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-bigquery",
"version": "3.0.4",
"version": "3.0.5",
"description": "A Google BigQuery language package for use with Open Function",
"main": "dist/index.cjs",
"scripts": {
Expand Down
7 changes: 7 additions & 0 deletions packages/cartodb/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-cartodb

## 0.4.6

### Patch Changes

- Updated dependencies [03a1a74]
- @openfn/language-common@2.1.0

## 0.4.5

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/cartodb/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-cartodb",
"version": "0.4.5",
"version": "0.4.6",
"description": "cartodb Language Pack for OpenFn",
"main": "dist/index.cjs",
"scripts": {
Expand Down
7 changes: 7 additions & 0 deletions packages/cht/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-cht

## 1.0.4

### Patch Changes

- Updated dependencies [03a1a74]
- @openfn/language-common@2.1.0

## 1.0.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/cht/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-cht",
"version": "1.0.3",
"version": "1.0.4",
"description": "OpenFn CHT adaptor",
"type": "module",
"exports": {
Expand Down
7 changes: 7 additions & 0 deletions packages/commcare/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-commcare

## 3.2.3

### Patch Changes

- Updated dependencies [03a1a74]
- @openfn/language-common@2.1.0

## 3.2.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/commcare/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-commcare",
"version": "3.2.2",
"version": "3.2.3",
"description": "Commcare Language Pack for OpenFn",
"homepage": "https://docs.openfn.org",
"repository": {
Expand Down
6 changes: 6 additions & 0 deletions packages/common/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## 2.0.0

## 2.1.0

### Minor Changes

- 03a1a74: Add `encode`, `decode` and `uuid` helpers

## 2.0.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-common",
"version": "2.0.3",
"version": "2.1.0",
"description": "Common Expressions for OpenFn",
"homepage": "https://docs.openfn.org",
"repository": {
Expand Down
37 changes: 37 additions & 0 deletions packages/common/src/util/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { randomUUID } from 'node:crypto';

/**
* Encodes a given string into Base64 format.
* @function
* @public
* @param {string} data - The string to be encoded.
* @returns {string} - The Base64 encoded string.
* @example <caption>Encode a string</caption>
* const encoded = encode('Hello World');
* console.log(encoded); // Output: SGVsbG8gV29ybGQ=
*/
export const encode = data => Buffer.from(data, 'utf-8').toString('base64');

/**
* Decodes a Base64 encoded string back to its original format.
* @function
* @public
* @param {string} base64Data - The Base64 encoded string.
* @returns {string} - The decoded string.
* @example <caption>Decode a Base64 string</caption>
* const decoded = decode('SGVsbG8gV29ybGQ=');
* console.log(decoded); // Output: Hello World
*/
export const decode = base64Data =>
Buffer.from(base64Data, 'base64').toString('utf-8');

/**
* Generates a UUID (Universally Unique Identifier).
* @function
* @public
* @returns {string} - A newly generated UUID.
* @example <caption>Generate a UUID</caption>
* const id = uuid();
* console.log(id); // Output:'3f4e254e-8f6f-4f8b-9651-1c1c262cc83f'
*/
export const uuid = () => randomUUID();
1 change: 1 addition & 0 deletions packages/common/src/util/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './http';
export * from './helpers';
export * from './references';
import parseDate from './parse-date';

Expand Down
59 changes: 59 additions & 0 deletions packages/common/test/util/helpers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { expect } from 'chai';
import { encode, decode, uuid } from '../../src/util/helpers.js';

describe('uuid', () => {
it('should generate a uuid', () => {
const uuidPattern =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;

expect(uuid()).to.match(uuidPattern);
});
it('should generate unique UUIDs', () => {
const id1 = uuid();
const id2 = uuid();

expect(id1).not.eql(id2);
});
});
const errorMsg =
'The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object';

describe('encode', () => {
it('should encode a string to Base64', () => {
expect(encode('Hello World')).to.eql('SGVsbG8gV29ybGQ=');
});

it('should encode an empty string to Base64', () => {
expect(encode('')).to.eql('');
});
it('should encode emoji to Base64', () => {
expect(encode('😀')).to.eql('8J+YgA==');
});
it('should throw an error if the string is not a string', () => {
expect(() => encode(123)).to.throw(errorMsg);
expect(() => encode(true)).to.throw(errorMsg);
expect(() => encode(null)).to.throw(errorMsg);
expect(() => encode({})).to.throw(errorMsg);
expect(() => encode(() => {})).to.throw(errorMsg);
});
});

describe('decode', () => {
it('should throw an error if the string is not a string', () => {
expect(() => decode(123)).to.throw(errorMsg);
expect(() => decode(true)).to.throw(errorMsg);
expect(() => decode(null)).to.throw(errorMsg);
expect(() => decode({})).to.throw(errorMsg);
expect(() => decode(() => {})).to.throw(errorMsg);
});
it('should decode a Base64 string for emoji', () => {
expect(decode('8J+YgA==')).to.eql('😀');
});
it('should decode a Base64 string back to its original string', () => {
expect(decode('SGVsbG8gV29ybGQ=')).to.eql('Hello World');
});

it('should decode an empty Base64 string', () => {
expect(decode('')).to.eql('');
});
});
7 changes: 7 additions & 0 deletions packages/dhis2/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-dhis2

## 5.0.5

### Patch Changes

- Updated dependencies [03a1a74]
- @openfn/language-common@2.1.0

## 5.0.4

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/dhis2/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-dhis2",
"version": "5.0.4",
"version": "5.0.5",
"description": "DHIS2 Language Pack for OpenFn",
"homepage": "https://docs.openfn.org",
"repository": {
Expand Down
7 changes: 7 additions & 0 deletions packages/dynamics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-dynamics

## 0.5.8

### Patch Changes

- Updated dependencies [03a1a74]
- @openfn/language-common@2.1.0

## 0.5.7

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/dynamics/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-dynamics",
"version": "0.5.7",
"version": "0.5.8",
"description": "A Microsoft Dynamics Language Pack for OpenFn",
"main": "dist/index.cjs",
"scripts": {
Expand All @@ -20,7 +20,7 @@
"configuration-schema.json"
],
"dependencies": {
"@openfn/language-common": "2.0.3",
"@openfn/language-common": "2.1.0",
"request": "^2.72.0"
},
"devDependencies": {
Expand Down
7 changes: 7 additions & 0 deletions packages/facebook/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-facebook

## 0.4.6

### Patch Changes

- Updated dependencies [03a1a74]
- @openfn/language-common@2.1.0

## 0.4.5

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/facebook/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-facebook",
"version": "0.4.5",
"version": "0.4.6",
"description": "An Language Package for Facebook Messenger API",
"main": "dist/index.cjs",
"scripts": {
Expand Down
7 changes: 7 additions & 0 deletions packages/fhir-ndr-et/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-fhir-ndr-et

## 0.1.3

### Patch Changes

- Updated dependencies [03a1a74]
- @openfn/language-common@2.1.0

## 0.1.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/fhir-ndr-et/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-fhir-ndr-et",
"version": "0.1.2",
"version": "0.1.3",
"description": "OpenFn fhir adaptor for NDR HIV in Ehtiopia",
"type": "module",
"exports": {
Expand Down
7 changes: 7 additions & 0 deletions packages/googlesheets/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-googlesheets

## 3.0.4

### Patch Changes

- Updated dependencies [03a1a74]
- @openfn/language-common@2.1.0

## 3.0.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/googlesheets/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-googlesheets",
"version": "3.0.3",
"version": "3.0.4",
"description": "A Google Sheets Language Pack for OpenFn",
"homepage": "https://docs.openfn.org",
"repository": {
Expand Down
7 changes: 7 additions & 0 deletions packages/hive/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-hive

## 0.3.6

### Patch Changes

- Updated dependencies [03a1a74]
- @openfn/language-common@2.1.0

## 0.3.5

### Patch Changes
Expand Down
Loading
Loading