Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dictcc

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dictcc - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

dist/cjs/languages.js

109

dist/cjs/dictcc.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var cheerio_1 = tslib_1.__importDefault(require("cheerio"));
var node_fetch_1 = tslib_1.__importDefault(require("node-fetch"));
var config_1 = require("./config");
var languages_1 = require("./languages");
var parser_1 = require("./parser");
var utils_1 = require("./utils");
exports.default = (function (_a) {
var fromLang = _a.fromLang, toLang = _a.toLang, term = _a.term;
return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var url, response, body, $_1, translateFrom_1, translateTo_1, error_1;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
if (!config_1.Languages[fromLang] || !config_1.Languages[toLang]) {
return [2, {
data: undefined,
error: new Error("Either ".concat(fromLang, " or ").concat(toLang, " value is not supported!")),
}];
}
_b.label = 1;
case 1:
_b.trys.push([1, 4, , 5]);
url = (0, utils_1.getDictccUrl)({ fromLang: fromLang, toLang: toLang, term: term });
return [4, (0, node_fetch_1.default)(url)];
case 2:
response = _b.sent();
return [4, response.text()];
case 3:
body = _b.sent();
$_1 = cheerio_1.default.load(body);
translateFrom_1 = [];
translateTo_1 = [];
$_1('td.td7nl').each(function (i, elem) {
var rawText = $_1(elem).text();
var text = (0, utils_1.getTranslatedText)(rawText);
var meta = (0, utils_1.getTextMeta)(rawText);
if (i % 2 === 0) {
translateTo_1.push({ text: text, meta: meta });
}
else {
translateFrom_1.push({ text: text, meta: meta });
}
});
exports.default = (function (input) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var sourceLanguage, targetLanguage, term, body, translations, _a, translationsLeft, translationsRight, data, error_1;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
sourceLanguage = input.sourceLanguage, targetLanguage = input.targetLanguage, term = input.term;
if (!languages_1.Languages[sourceLanguage] || !languages_1.Languages[targetLanguage]) {
return [2, {
data: translateFrom_1.map(function (element, index) { return ({
translateFrom: element,
translateTo: translateTo_1[index],
}); }),
data: undefined,
error: new Error("The language ".concat(sourceLanguage, " or ").concat(targetLanguage, " is not supported!")),
}];
}
_b.label = 1;
case 1:
_b.trys.push([1, 3, , 4]);
return [4, (0, parser_1.getHtmlString)(input)];
case 2:
body = _b.sent();
translations = (0, parser_1.getTranslationsArray)(body);
if (!translations[0] || !translations[1]) {
return [2, {
data: [],
error: undefined,
}];
case 4:
error_1 = _b.sent();
if (error_1 instanceof Error) {
return [2, {
data: undefined,
error: error_1,
}];
}
return [3, 5];
case 5: return [2, {
data: undefined,
error: new Error('Unknown error'),
}
_a = (0, parser_1.getTranslationsColumns)(body), translationsLeft = _a.translationsLeft, translationsRight = _a.translationsRight;
data = void 0;
if (translations[0].includes(term)) {
data = (0, utils_1.prepareData)(translationsRight, translationsLeft);
}
else {
data = (0, utils_1.prepareData)(translationsLeft, translationsRight);
}
return [2, {
data: data,
error: undefined,
}];
}
});
case 3:
error_1 = _b.sent();
if (error_1 instanceof Error) {
return [2, {
data: undefined,
error: error_1,
}];
}
return [3, 4];
case 4: return [2, {
data: undefined,
error: new Error('Unknown error'),
}];
}
});
});
}); });

@@ -9,3 +9,3 @@ "use strict";

Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(dictcc_1).default; } });
var config_1 = require("./config");
Object.defineProperty(exports, "Languages", { enumerable: true, get: function () { return config_1.Languages; } });
var languages_1 = require("./languages");
Object.defineProperty(exports, "Languages", { enumerable: true, get: function () { return languages_1.Languages; } });
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDictccUrl = exports.getTranslatedText = exports.getTextMeta = void 0;
var removeBrackets = function (text) { return text.replace(/\[|\]|\{|\}/g, ''); };
exports.prepareData = exports.getTranslatedText = exports.getTextMeta = void 0;
var getTextMeta = function (text) {
var _a;
return (_a = text
.match(/\[.+\]|\{.+\}/g)) === null || _a === void 0 ? void 0 : _a.map(function (t) {
if (t.includes('] [')) {
return t.split('] [').map(function (e) { return removeBrackets(e); });
}
return removeBrackets(t);
}).flat();
var abbreviations = Array.from(text.matchAll(/<(.*?)>/g), function (m) { return m[1]; });
var comments = Array.from(text.matchAll(/\[(.*?)\]/g), function (m) { return m[1]; });
var optionalData = Array.from(text.matchAll(/\((.*?)\)/g), function (m) { return m[1]; });
var wordClassDefinitions = Array.from(text.matchAll(/\{(.*?)\}/g), function (m) { return m[1]; });
return { abbreviations: abbreviations, comments: comments, optionalData: optionalData, wordClassDefinitions: wordClassDefinitions };
};

@@ -18,14 +14,12 @@ exports.getTextMeta = getTextMeta;

return text
.replace(/\d/g, '')
.replace(/\[.+\]/g, '')
.replace(/\{.+\}/g, '')
.replace(/\d|\[.+\]|<.+>|\{.+\}|\(.+\)/g, '')
.trim();
};
exports.getTranslatedText = getTranslatedText;
var getDictccUrl = function (_a) {
var fromLang = _a.fromLang, toLang = _a.toLang, term = _a.term;
var url = new URL("https://".concat(fromLang, "-").concat(toLang, ".dict.cc"));
url.searchParams.set('s', term);
return url.href;
var prepareData = function (from, to) {
return from.map(function (element, index) { return ({
translateFrom: element,
translateTo: to[index],
}); });
};
exports.getDictccUrl = getDictccUrl;
exports.prepareData = prepareData;
import { __awaiter, __generator } from "tslib";
import cheerio from 'cheerio';
import fetch from 'node-fetch';
import { Languages } from './config';
import { getDictccUrl, getTextMeta, getTranslatedText } from './utils';
export default (function (_a) {
var fromLang = _a.fromLang, toLang = _a.toLang, term = _a.term;
return __awaiter(void 0, void 0, void 0, function () {
var url, response, body, $_1, translateFrom_1, translateTo_1, error_1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (!Languages[fromLang] || !Languages[toLang]) {
return [2, {
data: undefined,
error: new Error("Either ".concat(fromLang, " or ").concat(toLang, " value is not supported!")),
}];
}
_b.label = 1;
case 1:
_b.trys.push([1, 4, , 5]);
url = getDictccUrl({ fromLang: fromLang, toLang: toLang, term: term });
return [4, fetch(url)];
case 2:
response = _b.sent();
return [4, response.text()];
case 3:
body = _b.sent();
$_1 = cheerio.load(body);
translateFrom_1 = [];
translateTo_1 = [];
$_1('td.td7nl').each(function (i, elem) {
var rawText = $_1(elem).text();
var text = getTranslatedText(rawText);
var meta = getTextMeta(rawText);
if (i % 2 === 0) {
translateTo_1.push({ text: text, meta: meta });
}
else {
translateFrom_1.push({ text: text, meta: meta });
}
});
import { Languages } from './languages';
import { getHtmlString, getTranslationsColumns, getTranslationsArray, } from './parser';
import { prepareData } from './utils';
export default (function (input) { return __awaiter(void 0, void 0, void 0, function () {
var sourceLanguage, targetLanguage, term, body, translations, _a, translationsLeft, translationsRight, data, error_1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
sourceLanguage = input.sourceLanguage, targetLanguage = input.targetLanguage, term = input.term;
if (!Languages[sourceLanguage] || !Languages[targetLanguage]) {
return [2, {
data: translateFrom_1.map(function (element, index) { return ({
translateFrom: element,
translateTo: translateTo_1[index],
}); }),
data: undefined,
error: new Error("The language ".concat(sourceLanguage, " or ").concat(targetLanguage, " is not supported!")),
}];
}
_b.label = 1;
case 1:
_b.trys.push([1, 3, , 4]);
return [4, getHtmlString(input)];
case 2:
body = _b.sent();
translations = getTranslationsArray(body);
if (!translations[0] || !translations[1]) {
return [2, {
data: [],
error: undefined,
}];
case 4:
error_1 = _b.sent();
if (error_1 instanceof Error) {
return [2, {
data: undefined,
error: error_1,
}];
}
return [3, 5];
case 5: return [2, {
data: undefined,
error: new Error('Unknown error'),
}
_a = getTranslationsColumns(body), translationsLeft = _a.translationsLeft, translationsRight = _a.translationsRight;
data = void 0;
if (translations[0].includes(term)) {
data = prepareData(translationsRight, translationsLeft);
}
else {
data = prepareData(translationsLeft, translationsRight);
}
return [2, {
data: data,
error: undefined,
}];
}
});
case 3:
error_1 = _b.sent();
if (error_1 instanceof Error) {
return [2, {
data: undefined,
error: error_1,
}];
}
return [3, 4];
case 4: return [2, {
data: undefined,
error: new Error('Unknown error'),
}];
}
});
});
}); });
export { default } from './dictcc';
export { Languages } from './config';
export { Languages } from './languages';

@@ -1,24 +0,18 @@

var removeBrackets = function (text) { return text.replace(/\[|\]|\{|\}/g, ''); };
export var getTextMeta = function (text) {
var _a;
return (_a = text
.match(/\[.+\]|\{.+\}/g)) === null || _a === void 0 ? void 0 : _a.map(function (t) {
if (t.includes('] [')) {
return t.split('] [').map(function (e) { return removeBrackets(e); });
}
return removeBrackets(t);
}).flat();
var abbreviations = Array.from(text.matchAll(/<(.*?)>/g), function (m) { return m[1]; });
var comments = Array.from(text.matchAll(/\[(.*?)\]/g), function (m) { return m[1]; });
var optionalData = Array.from(text.matchAll(/\((.*?)\)/g), function (m) { return m[1]; });
var wordClassDefinitions = Array.from(text.matchAll(/\{(.*?)\}/g), function (m) { return m[1]; });
return { abbreviations: abbreviations, comments: comments, optionalData: optionalData, wordClassDefinitions: wordClassDefinitions };
};
export var getTranslatedText = function (text) {
return text
.replace(/\d/g, '')
.replace(/\[.+\]/g, '')
.replace(/\{.+\}/g, '')
.replace(/\d|\[.+\]|<.+>|\{.+\}|\(.+\)/g, '')
.trim();
};
export var getDictccUrl = function (_a) {
var fromLang = _a.fromLang, toLang = _a.toLang, term = _a.term;
var url = new URL("https://".concat(fromLang, "-").concat(toLang, ".dict.cc"));
url.searchParams.set('s', term);
return url.href;
export var prepareData = function (from, to) {
return from.map(function (element, index) { return ({
translateFrom: element,
translateTo: to[index],
}); });
};
import { TranslationInput, TranslationResult } from './types';
declare const _default: ({ fromLang, toLang, term, }: TranslationInput) => Promise<{
declare const _default: (input: TranslationInput) => Promise<{
data: TranslationResult[] | undefined;

@@ -4,0 +4,0 @@ error: Error | undefined;

export { default } from './dictcc';
export { Languages } from './config';
export { Languages } from './languages';
export type { Translation, TranslationResult } from './types';

@@ -1,5 +0,11 @@

import { Languages } from './config';
import { Languages } from './languages';
export declare type TextMetaResult = {
abbreviations: string[];
comments: string[];
optionalData: string[];
wordClassDefinitions: string[];
};
export declare type Translation = {
text: string;
meta: string[] | undefined;
meta: TextMetaResult;
};

@@ -11,5 +17,5 @@ export declare type TranslationResult = {

export declare type TranslationInput = {
fromLang: Languages;
toLang: Languages;
sourceLanguage: Languages;
targetLanguage: Languages;
term: string;
};

@@ -1,4 +0,7 @@

import { TranslationInput } from './types';
export declare const getTextMeta: (text: string) => string[] | undefined;
import { TextMetaResult, Translation } from './types';
export declare const getTextMeta: (text: string) => TextMetaResult;
export declare const getTranslatedText: (text: string) => string;
export declare const getDictccUrl: ({ fromLang, toLang, term }: TranslationInput) => string;
export declare const prepareData: (from: Translation[], to: Translation[]) => {
translateFrom: Translation;
translateTo: Translation;
}[];
{
"name": "dictcc",
"version": "1.0.0",
"version": "1.1.0",
"description": "Unofficial dict.cc translation client",

@@ -17,2 +17,4 @@ "author": "Stefan Natter (https://natterstefan.me/)",

},
"main": "./dist/cjs/index.js",
"types": "./dist/types/index.d.ts",
"exports": {

@@ -24,3 +26,2 @@ ".": {

},
"types": "./dist/types/index.d.ts",
"files": [

@@ -54,2 +55,3 @@ "dist",

"@types/jest": "^27.4.1",
"all-contributors-cli": "^6.20.0",
"conventional-changelog-conventionalcommits": "^4.6.3",

@@ -56,0 +58,0 @@ "eslint": "^8.12.0",

# dictcc
[![npm version](https://badge.fury.io/js/dictcc.svg)](https://www.npmjs.com/package/dictcc)
[![Node CI](https://github.com/natterstefan/dictcc/actions/workflows/ci.yml/badge.svg)](https://github.com/natterstefan/dictcc/actions/workflows/ci.yml)
[![GitHub issues](https://img.shields.io/github/issues/natterstefan/dictcc)](https://github.com/natterstefan/dictcc/issues)
[![GitHub license](https://img.shields.io/github/license/natterstefan/dictcc)](https://github.com/natterstefan/dictcc/blob/main/LICENSE)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://github.com/natterstefan/dictcc/issues/new/choose)
`dictcc` is a client for the [dict.cc](https://dict.cc/) dictionary service. It

@@ -20,4 +26,4 @@ returns a list of translations for a given term for a given source and target

/**
* `translate` translates a `term` from a language `fromLang` into another
* language `toLang`.
* `translate` translates a `term` from a language (`sourceLanguage`) into another
* language (`targetLanguage`).
*

@@ -30,5 +36,5 @@ * `Languages` represents supported languages

const { data, error } = await translate({
fromLang: Languages.en,
toLang: Languages.de,
term: 'term',
sourceLanguage: Languages.en,
targetLanguage: Languages.de,
term: 'home',
})

@@ -40,4 +46,54 @@

A result looks something like this:
```json
[
{
"translateFrom": {
"text": "home",
"meta": {
"abbreviations": [],
"comments": ["at home"],
"optionalData": [],
"wordClassDefinitions": ["adv"]
}
},
"translateTo": {
"text": "daheim",
"meta": {
"abbreviations": [],
"comments": ["bes. südd., österr. u. schweiz."],
"optionalData": [],
"wordClassDefinitions": []
}
}
}
]
```
## License
[MIT](./LICENSE)
## Contributors ✨
Thanks goes to these wonderful people
([emoji key](https://allcontributors.org/docs/en/emoji-key)):
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tr>
<td align="center"><a href="https://natterstefan.me/"><img src="https://avatars.githubusercontent.com/u/1043668?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Stefan Natter</b></sub></a><br /><a href="#ideas-natterstefan" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/natterstefan/dictcc/commits?author=natterstefan" title="Code">💻</a></td>
</tr>
</table>
<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->
<!-- ALL-CONTRIBUTORS-LIST:END -->
This project follows the
[all-contributors](https://github.com/all-contributors/all-contributors)
specification. Contributions of any kind welcome!
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc