Skip to content

Commit af2bbfe

Browse files
committed
add prettier
1 parent c0ae426 commit af2bbfe

7 files changed

+84
-57
lines changed

.prettierignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/*
2+
dist/*

.prettierrc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
semi: false
2+
trailingComma: es5
3+
tabWidth: 4
4+
useTabs: false
5+
singleQuote: true
6+
bracketSpacing: true
7+
printWidth: 80

example.js

+28-20
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ const apikey = process.env.INTENTO_API_KEY
66
const google_api_key = process.env.GOOGLE_API_KEY
77

88
if (!apikey) {
9-
console.error('Missing Intento API key in the environment. Consider one of the options https://github.com/intento/intento-nodejs#how-to-pass-your-api-keys-to-your-environment')
9+
console.error(
10+
'Missing Intento API key in the environment. Consider one of the options https://github.com/intento/intento-nodejs#how-to-pass-your-api-keys-to-your-environment'
11+
)
1012
process.exit(1)
1113
}
1214

@@ -22,12 +24,15 @@ const client = new IntentoConnector({ apikey }, true) // debug mode is on
2224
// - source language will be detected automatically
2325
// - provider for the translation will be smart-selected based on the Smart routing feature
2426
// see more on that here https://github.com/intento/intento-api#smart-routing
25-
client.ai.text.translate.fullfill({ text: 'How\'s it going?', to: 'es' })
27+
client.ai.text.translate.fullfill({ text: "How's it going?", to: 'es' })
2628

2729
// Analyze text for sentiments
2830
// https://github.com/intento/intento-api/blob/master/ai.text.sentiment.md#basic-usage
29-
client.ai.text.sentiment.fullfill({ text: 'We love this', lang: 'en', provider: 'ai.text.sentiment.ibm.natural_language_understanding' })
30-
31+
client.ai.text.sentiment.fullfill({
32+
text: 'We love this',
33+
lang: 'en',
34+
provider: 'ai.text.sentiment.ibm.natural_language_understanding',
35+
})
3136

3237
/* Get information on providers */
3338

@@ -40,32 +45,39 @@ client.ai.text.translate.getProviders({ lang_detect: true }, (err, data) => {
4045
})
4146

4247
// Also source language (`from`), target language(`to`) and bulk mode feature availability (`bulk: true`) can be specified as params
43-
client.ai.text.translate.getProviders({ to: 'ru', bulk: true, lang_detect: true }, (err, data) => {
44-
console.log('\nProvider supporting bulk translation to russian with language detect feature')
45-
prettyPrintProviders(err, data)
46-
})
48+
client.ai.text.translate.getProviders(
49+
{ to: 'ru', bulk: true, lang_detect: true },
50+
(err, data) => {
51+
console.log(
52+
'\nProvider supporting bulk translation to russian with language detect feature'
53+
)
54+
prettyPrintProviders(err, data)
55+
}
56+
)
4757

4858
// Providers for sentiment analysys
4959
client.ai.text.sentiment.getProviders(prettyPrintProviders)
5060

5161
// Providers to get meanings of texts in selected languages
5262
client.ai.text.dictionary.getProviders(prettyPrintProviders)
5363

54-
5564
/* Advanced usage */
5665

5766
const sampleContext = {
58-
text: 'How\'s it going?',
67+
text: "How's it going?",
5968
from: 'en',
60-
to: 'es'
69+
to: 'es',
6170
}
6271

6372
// Translate with specific strategy
64-
client.ai.text.translate.withStrategy('best_price', sampleContext, (err, data) => {
65-
console.log('\nTranslate with a "best_price" strategy \n')
66-
defaultCallback(err, data)
67-
})
68-
73+
client.ai.text.translate.withStrategy(
74+
'best_price',
75+
sampleContext,
76+
(err, data) => {
77+
console.log('\nTranslate with a "best_price" strategy \n')
78+
defaultCallback(err, data)
79+
}
80+
)
6981

7082
/* Advanced general examples */
7183

@@ -87,7 +99,6 @@ client.makeRequest({
8799
},
88100
})
89101

90-
91102
// With custom google api key
92103
client.makeRequest({
93104
path: '/ai/text/translate',
@@ -112,8 +123,6 @@ client.makeRequest({
112123
},
113124
})
114125

115-
116-
117126
/* helpers */
118127

119128
function defaultCallback(err, data) {
@@ -124,7 +133,6 @@ function defaultCallback(err, data) {
124133
console.log('API response:\n', data, '\n\n')
125134
}
126135

127-
128136
function prettyPrintProviders(err, data) {
129137
if (err) {
130138
console.log('error:' + err.message)

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
"author": "Intento Inc.",
77
"license": "Apache-2.0",
88
"scripts": {
9-
"build": "webpack"
9+
"build": "webpack",
10+
"format": "prettier --config .prettierrc --write './**/*.js'"
1011
},
1112
"devDependencies": {
1213
"babel-eslint": "^8.2.2",
1314
"eslint": "^4.19.1",
15+
"prettier": "^1.12.1",
1416
"webpack": "^4.4.1",
1517
"webpack-cli": "^2.0.13"
1618
}

src/index.js

+39-35
Original file line numberDiff line numberDiff line change
@@ -17,67 +17,66 @@ function IntentoConnector(credentials = {}, debug = false) {
1717
if (!apikey) {
1818
console.error('Missing Intento API key')
1919
return {
20-
error: 'No Intento API key provided'
20+
error: 'No Intento API key provided',
2121
}
2222
}
2323

2424
this.options = {
2525
host: 'api.inten.to',
2626
headers: {
27-
apikey
28-
}
27+
apikey,
28+
},
2929
}
3030

3131
this.ai = Object.freeze({
3232
text: {
3333
translate: {
34-
fullfill: function (context, fn) {
34+
fullfill: function(context, fn) {
3535
this.fullfill('translate', context, fn)
3636
}.bind(this),
37-
getProviders: function (params, fn) {
37+
getProviders: function(params, fn) {
3838
this.getProviders('translate', params, fn)
3939
}.bind(this),
40-
withStrategy: function (strategy, context, fn) {
40+
withStrategy: function(strategy, context, fn) {
4141
this.withStrategy('translate', strategy, context, fn)
4242
}.bind(this),
4343
},
4444
sentiment: {
45-
fullfill: function (context, fn) {
45+
fullfill: function(context, fn) {
4646
this.fullfill('sentiment', context, fn)
4747
}.bind(this),
48-
getProviders: function (params, fn) {
48+
getProviders: function(params, fn) {
4949
this.getProviders('sentiment', params, fn)
5050
}.bind(this),
51-
withStrategy: function (strategy, context, fn) {
51+
withStrategy: function(strategy, context, fn) {
5252
if (this.debug) {
5353
console.warn('Experimental feature')
5454
}
5555
this.withStrategy('sentiment', strategy, context, fn)
5656
}.bind(this),
5757
},
5858
dictionary: {
59-
fullfill: function (context, fn) {
59+
fullfill: function(context, fn) {
6060
this.fullfill('dictionary', context, fn)
6161
}.bind(this),
62-
getProviders: function (params, fn) {
62+
getProviders: function(params, fn) {
6363
this.getProviders('dictionary', params, fn)
6464
}.bind(this),
65-
withStrategy: function (strategy, context, fn) {
65+
withStrategy: function(strategy, context, fn) {
6666
if (this.debug) {
6767
console.warn('Experimental feature')
6868
}
6969
this.withStrategy('dictionary', strategy, context, fn)
7070
}.bind(this),
7171
},
72-
}
72+
},
7373
})
7474
}
7575

7676
module.exports = IntentoConnector
7777

7878
module.exports.default = Object.assign({}, module.exports)
7979

80-
8180
IntentoConnector.prototype.makeRequest = function({
8281
path = '',
8382
params,
@@ -94,7 +93,9 @@ IntentoConnector.prototype.makeRequest = function({
9493
}
9594
}
9695
if (data && content) {
97-
console.warn('Specify either `data` or `content` to pass data to POST request. \n For now `data` will be used.')
96+
console.warn(
97+
'Specify either `data` or `content` to pass data to POST request. \n For now `data` will be used.'
98+
)
9899
}
99100
const urlParams = querystring.stringify(params)
100101
const settings = {
@@ -115,16 +116,15 @@ IntentoConnector.prototype.makeRequest = function({
115116
req.end()
116117
}
117118

118-
119-
IntentoConnector.prototype.fullfill = function (slug, context = {}, fn) {
119+
IntentoConnector.prototype.fullfill = function(slug, context = {}, fn) {
120120
const provider = context.provider
121121
delete context.provider
122122

123123
const content = { context }
124124

125125
if (slug === 'dictionary') {
126126
content.service = {
127-
provider: 'ai.text.dictionary.yandex.dictionary_api.1-0'
127+
provider: 'ai.text.dictionary.yandex.dictionary_api.1-0',
128128
}
129129
} else if (slug === 'sentiment') {
130130
if (!provider) {
@@ -134,13 +134,13 @@ IntentoConnector.prototype.fullfill = function (slug, context = {}, fn) {
134134
'\nProvider `id` is needed to be specified as a provider'
135135
)
136136
console.log('No request will be made')
137-
this.ai.text.sentiment.getProviders(function(err, data){
137+
this.ai.text.sentiment.getProviders(function(err, data) {
138138
if (!err) {
139139
console.log('Select one of the provider ids')
140140
data.forEach((p, i) => console.log(` ${i + 1}. ${p.id}`))
141141
}
142142
})
143-
return
143+
return
144144
}
145145

146146
content.service = { provider }
@@ -150,43 +150,45 @@ IntentoConnector.prototype.fullfill = function (slug, context = {}, fn) {
150150
path: getPath(slug, this.debug),
151151
content,
152152
method: 'POST',
153-
fn
153+
fn,
154154
})
155155
}
156156

157-
158-
IntentoConnector.prototype.getProviders = function (slug, params, fn) {
157+
IntentoConnector.prototype.getProviders = function(slug, params, fn) {
159158
if (params instanceof Function) {
160159
fn = params
161160
params = {}
162161
}
163-
162+
164163
this.makeRequest({
165164
path: getPath(slug, this.debug),
166165
params,
167166
method: 'GET',
168-
fn
167+
fn,
169168
})
170169
}
171170

172-
173-
IntentoConnector.prototype.withStrategy = function (slug, strategy, context, fn) {
171+
IntentoConnector.prototype.withStrategy = function(
172+
slug,
173+
strategy,
174+
context,
175+
fn
176+
) {
174177
const content = {
175178
context,
176179
service: {
177-
bidding: strategy
178-
}
180+
bidding: strategy,
181+
},
179182
}
180183

181184
this.makeRequest({
182185
path: getPath(slug, this.debug),
183186
content,
184187
method: 'POST',
185-
fn
188+
fn,
186189
})
187190
}
188191

189-
190192
// helpers
191193

192194
const pathBySlug = {
@@ -200,7 +202,9 @@ function getPath(slug, debug) {
200202
if (!path) {
201203
path = pathBySlug.translate
202204
if (debug) {
203-
console.error(`getProviders: Unknown intent ${slug}. Translate intent will be used`)
205+
console.error(
206+
`getProviders: Unknown intent ${slug}. Translate intent will be used`
207+
)
204208
}
205209
}
206210

@@ -218,10 +222,10 @@ function defaultCallback(err, data) {
218222
function response_handler(response, fn) {
219223
response.setEncoding('utf8')
220224
let body = ''
221-
response.on('data', function (chunk) {
225+
response.on('data', function(chunk) {
222226
body += chunk
223227
})
224-
response.on('end', function () {
228+
response.on('end', function() {
225229
try {
226230
let data = null
227231
if (body.length > 0) {
@@ -232,7 +236,7 @@ function response_handler(response, fn) {
232236
fn(e, null)
233237
}
234238
})
235-
response.on('error', function (e) {
239+
response.on('error', function(e) {
236240
console.log('Error: ' + e.message)
237241
})
238242
}

webpack.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ module.exports = {
66
filename: 'bundle.js',
77
path: path.resolve(__dirname, 'dist'),
88
},
9-
}
9+
}

yarn.lock

+4
Original file line numberDiff line numberDiff line change
@@ -3658,6 +3658,10 @@ preserve@^0.2.0:
36583658
version "0.2.0"
36593659
resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b"
36603660

3661+
prettier@^1.12.1:
3662+
version "1.12.1"
3663+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.12.1.tgz#c1ad20e803e7749faf905a409d2367e06bbe7325"
3664+
36613665
prettier@^1.5.3:
36623666
version "1.11.1"
36633667
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.11.1.tgz#61e43fc4cd44e68f2b0dfc2c38cd4bb0fccdcc75"

0 commit comments

Comments
 (0)