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

Add toUTF8 function in salesforce #441

Merged
merged 9 commits into from
Dec 1, 2023
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
6 changes: 6 additions & 0 deletions packages/salesforce/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @openfn/language-salesforce

## 4.3.0

### Minor Changes

- 1d5b62f: Add `toUTF8` function

## 4.2.2

### Patch Changes
Expand Down
38 changes: 38 additions & 0 deletions packages/salesforce/ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,44 @@
]
},
"valid": true
},
{
"name": "toUTF8",
"params": [
"input"
],
"docs": {
"description": "Transliterates unicode characters to their best ASCII representation",
"tags": [
{
"title": "public",
"description": null,
"type": null
},
{
"title": "example",
"description": "fn((state) => {\n const s = toUTF8(\"άνθρωποι\");\n console.log(s); // anthropoi\n return state;\n});"
},
{
"title": "param",
"description": "A string with unicode characters",
"type": {
"type": "NameExpression",
"name": "string"
},
"name": "input"
},
{
"title": "returns",
"description": "ASCII representation of input string",
"type": {
"type": "NameExpression",
"name": "String"
}
}
]
},
"valid": true
}
],
"exports": [],
Expand Down
3 changes: 2 additions & 1 deletion packages/salesforce/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-salesforce",
"version": "4.2.2",
"version": "4.3.0",
"description": "Salesforce Language Pack for OpenFn",
"homepage": "https://docs.openfn.org",
"exports": {
Expand Down Expand Up @@ -32,6 +32,7 @@
],
"dependencies": {
"@openfn/language-common": "workspace:*",
"any-ascii": "^0.3.2",
"axios": "^0.21.4",
"jsforce": "^1.11.1",
"lodash": "^4.17.21"
Expand Down
24 changes: 24 additions & 0 deletions packages/salesforce/src/Adaptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ import { expandReferences as newExpandReferences } from '@openfn/language-common
import jsforce from 'jsforce';
import flatten from 'lodash/flatten';

// use a dynamic import because any-ascii is pure ESM and doesn't play well with CJS
// Note that technically we should await this, but in practice the module will be loaded
// before execute is called
let anyAscii = undefined;
import('any-ascii').then(m => {
anyAscii = m.default;
});

/**
* Adds a lookup relation or 'dome insert' to a record.
* @public
Expand Down Expand Up @@ -759,6 +767,22 @@ export function steps(...operations) {
return flatten(operations);
}

/**
* Transliterates unicode characters to their best ASCII representation
* @public
* @example
* fn((state) => {
* const s = toUTF8("άνθρωποι");
* console.log(s); // anthropoi
* return state;
* });
* @param {string} input - A string with unicode characters
* @returns {String} - ASCII representation of input string
*/
export function toUTF8(input) {
return anyAscii(input);
}

// Note that we expose the entire axios package to the user here.
import axios from 'axios';

Expand Down
17 changes: 17 additions & 0 deletions packages/salesforce/test/Adaptor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
createIf,
upsert,
upsertIf,
toUTF8,
steps,
each,
field,
Expand Down Expand Up @@ -170,4 +171,20 @@ describe('Adaptor', () => {
.catch(done);
});
});

describe('toUTF8', () => {
it('Transliterate unicode to ASCII representation', () => {
expect(toUTF8('άνθρωποι')).to.eql('anthropoi');
// Misc
expect(toUTF8('☆ ♯ ♰ ⚄ ⛌')).to.equal('* # + 5 X');
// Emojis
expect(toUTF8('👑 🌴')).to.eql(':crown: :palm_tree:');
// Letterlike
expect(toUTF8('№ ℳ ⅋ ⅍')).to.eql('No M & A/S');
// Ordinal coordinator
expect(toUTF8('Nhamaonha 6ª Classe 2023-10-09')).to.eql(
'Nhamaonha 6a Classe 2023-10-09'
);
});
});
});
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.