Skip to content

Commit

Permalink
⚒️ Applied ESLint rules / Tiny text fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
TemaSM committed Jul 18, 2019
1 parent 50b67d3 commit f47f6a5
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 46 deletions.
38 changes: 19 additions & 19 deletions lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ const
* Represents a wrapper around locally stored session, it's {@link LocalSession#middleware|middleware} & lowdb
*
* @param {Object} [options] - Options for {@link LocalSession|LocalSession} & {@link https://github.com/typicode/lowdb|lowdb}
* @param {String} [options.database] - Name or path to database file `(default: 'sessions.json')`
* @param {String} [options.property] - Name of property in {@link https://telegraf.js.org/#/?id=context|Telegraf Context} where session object will be located `(default: 'session')`
* @param {Object} [options.state] - Initial state of database. You can use it to pre-init database Arrays/Objects to store your own data `(default: {})`
* @param {String} [options.database] - Name or path to database file `default:` `'sessions.json'`
* @param {String} [options.property] - Name of property in {@link https://telegraf.js.org/#/?id=context|Telegraf Context} where session object will be located `default:` `'session'`
* @param {Object} [options.state] - Initial state of database. You can use it to pre-init database Arrays/Objects to store your own data `default:` `{}`
* @param {Function} [options.getSessionKey] - Function to get identifier for session from {@link https://telegraf.js.org/#/?id=context|Telegraf Context} (may implement it on your own)
* @param {Object} [options.storage] - lowdb storage option for implementing your own storage read/write operations `(default: {@link LocalSession.storageFileSync|LocalSession.storageFileSync})`
* @param {Object} [options.storage] - lowdb storage option for implementing your own storage read/write operations `default:` {@link LocalSession.storageFileSync|LocalSession.storageFileSync}
* @param {Function} [options.storage.read] - lowdb storage read function, must return an object or a Promise
* @param {Function} [options.storage.write] - lowdb storage write function, must return undefined or a Promise
* @param {Object} [options.format] - lowdb storage format option for implementing your own database format for read/write operations
* @param {Function} [options.format.serialize] - lowdb storage serialize function, must return data (usually string) `(default: {@link https://goo.gl/dmGpZd|JSON.stringify})`
* @param {Function} [options.format.deserialize] - lowdb storage serialize function, must return an object `(default: {@link https://goo.gl/wNy3ar|JSON.parse})`
* @param {Function} [options.format.serialize] - lowdb storage serialize function, must return data (usually string) `default:` {@link https://goo.gl/dmGpZd|JSON.stringify()}
* @param {Function} [options.format.deserialize] - lowdb storage deserialize function, must return an object `default:` {@link https://goo.gl/wNy3ar|JSON.parse()}
* @returns Instance of {@link LocalSession|LocalSession}
*/
class LocalSession {
Expand Down Expand Up @@ -47,7 +47,7 @@ class LocalSession {
this.DB = undefined
this._adapter = undefined
// DISABLED: this.options.defaultValue = this.options.state // Backward compatability with old lowdb
let defaultAdaptersOptions = Object.assign(
const defaultAdaptersOptions = Object.assign(
{ defaultValue: this.options.state },
this.options.format.serialize ? { serialize: this.options.format.serialize } : {},
this.options.format.deserialize ? { deserialize: this.options.format.deserialize } : {}
Expand All @@ -66,7 +66,7 @@ class LocalSession {
this._adapter = new this.options.storage(this.options.database, defaultAdaptersOptions)
}
debug('Initiating: lowdb instance')
let DbInstance = lowdb(this._adapter)
const DbInstance = lowdb(this._adapter)
// If lowdb initiated with async (Promise) adapter
if (isPromise(DbInstance)) {
debug('DbInstance is Promise like')
Expand Down Expand Up @@ -104,7 +104,7 @@ class LocalSession {
*/
getSession (key) {
this._called(arguments)
let session = this.DB.get('sessions').getById(key).value() || {}
const session = this.DB.get('sessions').getById(key).value() || {}
debug('Session state', session)
return session.data || {}
}
Expand All @@ -129,7 +129,7 @@ class LocalSession {
// If database has record, then just update it
if (this.DB.get('sessions').getById(key).value()) {
debug(' -> Updating')
let session = this.DB.get('sessions').updateById(key, { data: data }).write()
const session = this.DB.get('sessions').updateById(key, { data: data }).write()
// Check if lowdb Storage returned var type is Promise-like or just sync-driven data
if (isPromise(session)) {
return session.then(sess => { return sess })
Expand All @@ -141,7 +141,7 @@ class LocalSession {
// If no, so we should push new record into it
else {
debug(' -> Inserting')
let session = this.DB.get('sessions').push({ id: key, data: data }).write()
const session = this.DB.get('sessions').push({ id: key, data: data }).write()
// Check if lowdb Storage returned var type is Promise-like or just sync-driven data
if (isPromise(session)) {
return session.then(_session => { return _session[0] })
Expand All @@ -160,7 +160,7 @@ class LocalSession {
* @returns {Promise}
*/
middleware (property = this.options.property) {
let that = this
const that = this
return (ctx, next) => {
const key = that.getSessionKey(ctx)
if (!key) return next()
Expand All @@ -187,7 +187,7 @@ class LocalSession {
}

/**
* lowdb storage named `{@link https://git.io/vhM3Y|fileSync}` before `{@link https://git.io/vhM3Z|lowdb@0.17.0}`
* lowdb storage named {@link https://git.io/vhM3Y|fileSync} before {@link https://git.io/vhM3Z|lowdb@0.17.0}
*
* @memberof! LocalSession
* @name LocalSession.storagefileSync
Expand All @@ -199,7 +199,7 @@ class LocalSession {
}

/**
* lowdb storage/adapter named `{@link https://git.io/vhMqc|FileSync}`
* lowdb storage/adapter named {@link https://git.io/vhMqc|FileSync}
*
* @memberof! LocalSession
* @name LocalSession.storageFileSync
Expand All @@ -211,7 +211,7 @@ class LocalSession {
}

/**
* lowdb storage named `{@link https://git.io/vhM3m|fileAsync}` before `{@link https://git.io/vhM3Z|lowdb@0.17.0}`
* lowdb storage named {@link https://git.io/vhM3m|fileAsync} before {@link https://git.io/vhM3Z|lowdb@0.17.0}
*
* @memberof! LocalSession
* @name LocalSession.storagefileAsync
Expand All @@ -223,7 +223,7 @@ class LocalSession {
}

/**
* lowdb storage/adapter named `{@link https://git.io/vhMqm|FileAsync}`
* lowdb storage/adapter named {@link https://git.io/vhMqm|FileAsync}
*
* @memberof! LocalSession
* @name LocalSession.storageFileAsync
Expand All @@ -235,7 +235,7 @@ class LocalSession {
}

/**
* lowdb storage/adapter named `{@link https://git.io/vhMqs|Memory}`
* lowdb storage/adapter named {@link https://git.io/vhMqs|Memory}
*
* @memberof! LocalSession
* @name LocalSession.storageMemory
Expand All @@ -247,7 +247,7 @@ class LocalSession {
}

/**
* lowdb `{@link https://git.io/vhMOK|storage/adapter base}` constructor (to extend it in your custom storage/adapter)
* lowdb {@link https://git.io/vhMOK|storage/adapter base} constructor (to extend it in your custom storage/adapter)
*
* @memberof! LocalSession
* @name LocalSession.storageBase
Expand Down Expand Up @@ -286,7 +286,7 @@ function isPromise (obj) {
/**
* @overview {@link http://telegraf.js.org/|Telegraf} Session middleware for storing sessions locally (Memory/FileSync/FileAsync/...)
* @module telegraf-session-local
* @version 0.0.7
* @version 1.0.0
* @license MIT
* @author Tema Smirnov <git.tema@smirnov.one>
* @requires {@link https://www.npmjs.com/package/telegraf|npm: telegraf}
Expand Down
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "lib/session.js",
"repository": {
"type": "git",
"url": "https://github.com/realspeaker/telegraf-session-local.git"
"url": "git+https://github.com/RealSpeaker/telegraf-session-local.git"
},
"keywords": [
"telegram",
Expand All @@ -27,7 +27,7 @@
"Edgar Toll <edjopato@gmail.com> (https://edjopato.de/)"
],
"bugs": {
"url": "https://github.com/realspeaker/telegraf-session-local/issues"
"url": "https://github.com/RealSpeaker/telegraf-session-local/issues"
},
"homepage": "https://realspeaker.github.io/telegraf-session-local/",
"engines": {
Expand All @@ -40,7 +40,9 @@
"lint": "eslint . --fix",
"test": "nyc mocha tests --require should",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"jsdoc": "node_modules/.bin/jsdoc -c jsdoc.json"
"jsdoc": "node_modules/.bin/jsdoc -c jsdoc.json",
"version": "npm run jsdoc && git add docs",
"release": "np"
},
"dependencies": {
"lowdb": "^1.0.0"
Expand All @@ -61,8 +63,12 @@
"eslint-plugin-standard": "^4.0.0",
"jsdoc": "3.6.3",
"mocha": "6.1.4",
"np": "*",
"nyc": "^14.1.1",
"should": "^13.2.3",
"telegraf": "^2.0.0 || ^3.0.0"
},
"np": {
"yarn": false
}
}
8 changes: 4 additions & 4 deletions tests/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ const

describe('Telegraf Session local : General', () => {
let bot = {}
let localSession = new LocalSession(options)
const localSession = new LocalSession(options)

it('Should works without specifying any options for LocalSession', (done) => {
bot = new Telegraf()
let session = new LocalSession()
const session = new LocalSession()
bot.on('text', session.middleware(), (ctx) => {
should.exist(ctx.session)
done()
Expand All @@ -25,7 +25,7 @@ describe('Telegraf Session local : General', () => {

it('Should use custom `format.serialize` and `format.deserialize` functions', (done) => {
bot = new Telegraf()
let session = new LocalSession({
const session = new LocalSession({
database: 'test_sync_db.json',
storage: LocalSession.storageFileSync,
format: {
Expand Down Expand Up @@ -90,7 +90,7 @@ describe('Telegraf Session local : General', () => {
it('Should return `undefined` when no key provided for session to be saved', (done) => {
bot = new Telegraf()
bot.on('text', localSession.middleware(), (ctx) => {
let sessionKey = localSession.getSessionKey(ctx)
const sessionKey = localSession.getSessionKey(ctx)
debug('Real session key calculated by LocalSession: %s', sessionKey)
should.not.exists(localSession.saveSession(undefined, { authenticated: false }))
done()
Expand Down
10 changes: 5 additions & 5 deletions tests/lodash-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('lodash + lodash-id', () => {
}, /duplicate/)
should.equal(db.posts.length, length)
should.deepEqual(_.getById(db.posts, 2), { id: 2, body: 'two', published: false })
should.deepEqual(_.map(db.posts, 'id'), [ 1, 2, 3 ])
should.deepEqual(_.map(db.posts, 'id'), [1, 2, 3])
})
})

Expand All @@ -129,7 +129,7 @@ describe('lodash + lodash-id', () => {

it('replaces in place and returns inserted doc', () => {
const length = db.posts.length
const doc = _.upsert(db.posts, {id: 2, title: 'one'})
const doc = _.upsert(db.posts, { id: 2, title: 'one' })

should.equal(db.posts.length, length)
should.deepEqual(doc, { id: 2, title: 'one' })
Expand All @@ -140,7 +140,7 @@ describe('lodash + lodash-id', () => {

describe('and id is not set', () => {
it('inserts, sets an id and returns inserted doc', () => {
const doc = _.upsert(db.posts, {body: 'one'})
const doc = _.upsert(db.posts, { body: 'one' })

should.equal(db.posts.length, 4)
should(doc.id)
Expand Down Expand Up @@ -188,13 +188,13 @@ describe('lodash + lodash-id', () => {

describe('__update', () => {
it('copies properties from an object to another', () => {
let src = {
const src = {
1: 'one',
test: true,
hello: 'world',
leet: 1337
}
let dst = {}
const dst = {}
_.__update(dst, src)

dst.should.containDeep(src)
Expand Down
2 changes: 1 addition & 1 deletion tests/sessionKeyUpdateTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const

describe('Telegraf Session local : Session Key Update Types', () => {
let bot = {}
let localSession = new LocalSession(options)
const localSession = new LocalSession(options)

it('Should handle message', (done) => {
bot = new Telegraf()
Expand Down
6 changes: 4 additions & 2 deletions tests/storageCustom.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ class storageCustom extends LocalSession.storageBase {
super()
this.serialize = require('lowdb/adapters/_stringify')
}

read () { return this.defaultValue }

write () {}
}

const options = { storage: storageCustom }

describe('Telegraf Session local : storageCustom', () => {
let bot = {}
let localSession = new LocalSession(options)
const localSession = new LocalSession(options)

it('storageCustom: Should retrieve and save session', (done) => {
const key = '1:1' // ChatID:FromID
let session = localSession.getSession(key)
const session = localSession.getSession(key)
debug('getSession %O', session)
should.exist(session)
session.foo = 42
Expand Down
10 changes: 5 additions & 5 deletions tests/storageFileAsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const
options = { database: 'test_async_db.json', storage: LocalSession.storageFileAsync }

let bot = {}
let localSession = new LocalSession(options)
const localSession = new LocalSession(options)

// Wait for database async initialization finished
before((done) => {
Expand All @@ -18,7 +18,7 @@ describe('Telegraf Session local : storageFileAsync', () => {

it('storageFileAsync: Should retrieve and save session', (done) => {
const key = '1:1' // ChatID:FromID
let session = localSession.getSession(key)
const session = localSession.getSession(key)
debug('getSession %O', session)
should.exist(session)
session.foo = 42
Expand Down Expand Up @@ -79,13 +79,13 @@ describe('Telegraf Session local : storageFileAsync', () => {
})

it('storageFileAsync: Should work properly with deprecated stoarge name - storagefileAsync', (done) => {
let _options = Object.assign({ storage: LocalSession.storagefileAsync }, options)
let _localSession = new LocalSession(_options)
const _options = Object.assign({ storage: LocalSession.storagefileAsync }, options)
const _localSession = new LocalSession(_options)
// Wait for database async initialization finished
_localSession.DB.then((DB) => {
// console.log(DB.get('sessions').getById('1:1').value())
const key = '1:1' // ChatID:FromID
let session = _localSession.getSession(key)
const session = _localSession.getSession(key)
debug('getSession %O', session)
should.exist(session)
session.foo = 42
Expand Down
10 changes: 5 additions & 5 deletions tests/storageFileSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ const
options = { database: 'test_sync_db.json', storage: LocalSession.storageFileSync }

let bot = {}
let localSession = new LocalSession(options)
const localSession = new LocalSession(options)

describe('Telegraf Session local : storageFileSync', () => {

it('storageFileSync: Should retrieve and save session', (done) => {
const key = '1:1' // ChatID:FromID
let session = localSession.getSession(key)
const session = localSession.getSession(key)
debug('getSession %O', session)
should.exist(session)
session.foo = 42
Expand Down Expand Up @@ -74,10 +74,10 @@ describe('Telegraf Session local : storageFileSync', () => {
})

it('storageFileSync: Should work properly with deprecated stoarge name - storagefileSync', (done) => {
let _options = Object.assign({ storage: LocalSession.storagefileSync }, options)
let _localSession = new LocalSession(_options)
const _options = Object.assign({ storage: LocalSession.storagefileSync }, options)
const _localSession = new LocalSession(_options)
const key = '1:1' // ChatID:FromID
let session = _localSession.getSession(key)
const session = _localSession.getSession(key)
debug('getSession %O', session)
should.exist(session)
session.foo = 42
Expand Down
4 changes: 2 additions & 2 deletions tests/storageMemory.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ const

describe('Telegraf Session local : storageMemory', () => {
let bot = {}
let localSession = new LocalSession(options)
const localSession = new LocalSession(options)

it('storageMemory: Should retrieve and save session', (done) => {
const key = '1:1' // ChatID:FromID
let session = localSession.getSession(key)
const session = localSession.getSession(key)
debug('getSession %O', session)
should.exist(session)
session.foo = 42
Expand Down

0 comments on commit f47f6a5

Please sign in to comment.