Skip to content

Commit

Permalink
refactor: less lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Jan 14, 2024
1 parent 84bc0f8 commit 34aeb29
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
6 changes: 2 additions & 4 deletions src/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export default ({ serialize, deserialize, plans, redis } = {}) => {
key.value = await uid({ redis, size: 16 })
if (key.enabled === undefined) key.enabled = true
if (opts.plan) await plans.retrieve(opts.plan, { throwError: true })
await redis.setnx(key.id, serialize(key))
return key
return (await redis.setnx(key.id, serialize(key))) && key
}

/**
Expand Down Expand Up @@ -100,8 +99,7 @@ export default ({ serialize, deserialize, plans, redis } = {}) => {
})
if (Object.keys(metadata).length) key.metadata = metadata
if (key.plan) await plans.retrieve(key.plan, { throwError: true })
await redis.set(keyId, serialize(key))
return key
return (await redis.set(keyId, serialize(key))) && key
}

/**
Expand Down
11 changes: 4 additions & 7 deletions src/plans.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ export default ({ serialize, deserialize, redis } = {}) => {
const plan = pick(opts, PLAN_FIELDS.concat(PLAN_FIELDS_OBJECT))
plan.id = await uid({ redis, prefix: PLAN_PREFIX, size: 5 })
plan.createdAt = plan.updatedAt = Date.now()
await redis.setnx(plan.id, serialize(plan))
return plan
return (await redis.setnx(plan.id, serialize(plan))) && plan
}

/**
Expand Down Expand Up @@ -71,9 +70,8 @@ export default ({ serialize, deserialize, redis } = {}) => {
* @returns {boolean} Whether the plan was deleted or not.
*/
const del = async planId => {
const isDeleted = Boolean(
await redis.del(getKey(planId, { validate: true }))
)
const isDeleted =
(await redis.del(getKey(planId, { validate: true }))) === 1
if (!isDeleted) {
throw new TypeError(`The plan \`${planId}\` does not exist.`)
}
Expand Down Expand Up @@ -106,8 +104,7 @@ export default ({ serialize, deserialize, redis } = {}) => {
updatedAt: Date.now()
})
if (Object.keys(metadata).length) plan.metadata = metadata
await redis.set(planId, serialize(plan))
return plan
return (await redis.set(planId, serialize(plan))) && plan
}

/**
Expand Down

0 comments on commit 34aeb29

Please sign in to comment.