From b719ae1771885dd49d0f54bb69edd5fb3c1235f5 Mon Sep 17 00:00:00 2001 From: snyk-bot Date: Mon, 24 Feb 2020 02:03:05 +0000 Subject: [PATCH 1/2] fix: upgrade date-fns from 2.0.0-beta.3 to 2.9.0 Snyk has created this PR to upgrade date-fns from 2.0.0-beta.3 to 2.9.0. See this package in NPM: https://www.npmjs.com/package/date-fns See this project in Snyk: https://app.snyk.io/org/moranje/project/559be658-7e55-4fc1-88d3-29dcb1f6566a?utm_source=github&utm_medium=upgrade-pr --- package-lock.json | 6 +++--- package.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 565cd62a..41a29741 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4901,9 +4901,9 @@ } }, "date-fns": { - "version": "2.0.0-beta.3", - "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.0.0-beta.3.tgz", - "integrity": "sha512-z5O262BvHPhwUvA1weXH+AZodygnZUcORERw8hjwBUrRPGrAo2e/rjXfC8Ykf1OGJZGDuLnK/WXbEZBIc0exGQ==" + "version": "2.9.0", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.9.0.tgz", + "integrity": "sha512-khbFLu/MlzLjEzy9Gh8oY1hNt/Dvxw3J6Rbc28cVoYWQaC1S3YI4xwkF9ZWcjDLscbZlY9hISMr66RFzZagLsA==" }, "dateformat": { "version": "3.0.3", diff --git a/package.json b/package.json index c1b09414..b2966431 100644 --- a/package.json +++ b/package.json @@ -106,7 +106,7 @@ "dependencies": { "ajv": "6.10.2", "babel-polyfill": "6.26.0", - "date-fns": "2.0.0-beta.3", + "date-fns": "2.9.0", "fast-plist": "0.1.2", "fuzzaldrin": "2.1.0", "got": "9.6.0", From 86e426fcf57bbc56479d15f676c56a4ec2437810 Mon Sep 17 00:00:00 2001 From: codefactor-io Date: Mon, 24 Feb 2020 02:04:09 +0000 Subject: [PATCH 2/2] [CodeFactor] Apply fixes to commit b719ae1 [ci skip] [skip ci] --- src/project/settings-schema.ts | 4 +- src/project/settings.ts | 69 ++++--- src/todoist/label.ts | 41 ++-- src/todoist/project.ts | 43 ++-- src/todoist/query.ts | 53 ++--- src/todoist/rest-api-v8.ts | 348 ++++++++++++++++----------------- src/todoist/task.ts | 115 ++++++----- src/workflow/item.ts | 81 ++++---- src/workflow/list.ts | 23 +-- 9 files changed, 375 insertions(+), 402 deletions(-) diff --git a/src/project/settings-schema.ts b/src/project/settings-schema.ts index e9622ed1..4355679d 100644 --- a/src/project/settings-schema.ts +++ b/src/project/settings-schema.ts @@ -6,7 +6,7 @@ export interface Schema { description: string type: string properties: Properties - required?: (string)[] | null + required?: string[] | null } /** @hidden */ @@ -32,7 +32,7 @@ export interface Language { description: string explanation: string type: string - enum?: (string)[] | null + enum?: string[] | null } /** @hidden */ diff --git a/src/project/settings.ts b/src/project/settings.ts index cb9e1569..c610b3dd 100644 --- a/src/project/settings.ts +++ b/src/project/settings.ts @@ -1,34 +1,31 @@ -import { AlfredError, FILES, Schema, SETTINGS_PATH } from '@/project'; -import { Item, List, Notification, uuid } from '@/workflow'; -import AJV from 'ajv'; -import compose from 'stampit'; -import writeJsonFile from 'write-json-file'; +import { AlfredError, FILES, Schema, SETTINGS_PATH } from '@/project' +import { Item, List, Notification, uuid } from '@/workflow' +import AJV from 'ajv' +import compose from 'stampit' +import writeJsonFile from 'write-json-file' /** @hidden */ const ajv = new AJV({ allErrors: true }) /** @hidden */ -const SettingsList = compose( - List, - { - init(this: workflow.ListInstance, { settings }: { settings: project.Settings }) { - this.items = this.items || [] - - Object.keys(Schema.properties).forEach((key: string) => { - if (key !== 'uuid') { - this.items.push( - Item({ - title: `SETTING: ${key}`, - subtitle: `${Schema.properties[key].explanation}`, - autocomplete: ` ${key}`, - valid: false - }) - ) - } - }) - } +const SettingsList = compose(List, { + init(this: workflow.ListInstance, { settings }: { settings: project.Settings }) { + this.items = this.items || [] + + Object.keys(Schema.properties).forEach((key: string) => { + if (key !== 'uuid') { + this.items.push( + Item({ + title: `SETTING: ${key}`, + subtitle: `${Schema.properties[key].explanation}`, + autocomplete: ` ${key}`, + valid: false + }) + ) + } + }) } -) +}) /** @hidden */ const SettingList = compose(List, { @@ -122,7 +119,7 @@ function createDefault() { uuid: uuid(), anonymous_statistics: true } - let validate = ajv.compile(Schema) + const validate = ajv.compile(Schema) if (validate(starter)) { return starter @@ -133,7 +130,7 @@ function createDefault() { /** @hidden */ function castSettingTypes(settings: project.Settings) { - let typeCast: project.Settings = settings + const typeCast: project.Settings = settings Object.entries(settings).forEach(([key, value]) => { if (Schema.properties[key] && Schema.properties[key].type === 'boolean') { @@ -164,8 +161,8 @@ function castSettingTypes(settings: project.Settings) { /** @hidden */ function getErrors(key: string, value: string | number | boolean, settings: project.Settings) { - let updated = Object.assign({}, settings, { [key]: value }) - let validate = ajv.compile(Schema) + const updated = Object.assign({}, settings, { [key]: value }) + const validate = ajv.compile(Schema) if (!validate(castSettingTypes(updated))) { return validate.errors || [] @@ -180,28 +177,28 @@ export function getSettings(): project.Settings { } export function getSetting(setting: string) { - let settings = getSettings() + const settings = getSettings() return settings[setting] } export function list() { - let settings = getSettings() - let settingsList = SettingsList({ settings }) + const settings = getSettings() + const settingsList = SettingsList({ settings }) return settingsList.write() } export function verify(key: string, value: string | number | boolean) { - let settings = getSettings() - let settingList = SettingList({ key, value, settings }) + const settings = getSettings() + const settingList = SettingList({ key, value, settings }) return settingList.write() } export async function save({ key, value }: { key: string; value: string | number | boolean }) { - let settings = getSettings() - let errors = getErrors(key, value, settings) + const settings = getSettings() + const errors = getErrors(key, value, settings) if (errors.length === 0) { await writeJsonFile(SETTINGS_PATH, Object.assign(settings, { [key]: value })) diff --git a/src/todoist/label.ts b/src/todoist/label.ts index 2502ca35..246eb3c3 100644 --- a/src/todoist/label.ts +++ b/src/todoist/label.ts @@ -1,6 +1,6 @@ -import { AlfredError } from '@/project'; -import { Item, List } from '@/workflow'; -import compose from 'stampit'; +import { AlfredError } from '@/project' +import { Item, List } from '@/workflow' +import compose from 'stampit' /** @hidden */ export const Label: todoist.LabelFactory = compose({ @@ -22,23 +22,20 @@ export const Label: todoist.LabelFactory = compose({ }) /** @hidden */ -export const LabelList: todoist.LabelListFactory = compose( - List, - { - init( - this: todoist.LabelListInstance, - { labels = [], query }: { labels: todoist.Label[]; query: string } - ) { - labels.forEach((label: todoist.Label) => { - this.items.push( - Item({ - title: label.name, - subtitle: `Add label ${label.name} to task`, - autocomplete: `${query.replace(/(^.*@).*/, '$1')}${label.name} `, - valid: false - }) - ) - }) - } +export const LabelList: todoist.LabelListFactory = compose(List, { + init( + this: todoist.LabelListInstance, + { labels = [], query }: { labels: todoist.Label[]; query: string } + ) { + labels.forEach((label: todoist.Label) => { + this.items.push( + Item({ + title: label.name, + subtitle: `Add label ${label.name} to task`, + autocomplete: `${query.replace(/(^.*@).*/, '$1')}${label.name} `, + valid: false + }) + ) + }) } -) +}) diff --git a/src/todoist/project.ts b/src/todoist/project.ts index d9b64cbb..4977b246 100644 --- a/src/todoist/project.ts +++ b/src/todoist/project.ts @@ -1,6 +1,6 @@ -import { AlfredError } from '@/project'; -import { Item, List } from '@/workflow'; -import compose from 'stampit'; +import { AlfredError } from '@/project' +import { Item, List } from '@/workflow' +import compose from 'stampit' /** @hidden */ export const Project: todoist.ProjectFactory = compose({ @@ -18,25 +18,22 @@ export const Project: todoist.ProjectFactory = compose({ }) /** @hidden */ -export const ProjectList: todoist.ProjectListFactory = compose( - List, - { - init( - this: todoist.ProjectListInstance, - { projects = [], query }: { projects: todoist.Project[]; query: string } - ) { - projects.forEach((project: todoist.Project) => { - let name = project.name.indexOf(' ') !== -1 ? `[${project.name}]` : project.name +export const ProjectList: todoist.ProjectListFactory = compose(List, { + init( + this: todoist.ProjectListInstance, + { projects = [], query }: { projects: todoist.Project[]; query: string } + ) { + projects.forEach((project: todoist.Project) => { + const name = project.name.indexOf(' ') !== -1 ? `[${project.name}]` : project.name - this.items.push( - Item({ - title: project.name, - subtitle: `Move task to ${project.name}`, - autocomplete: `${query.replace(/(^.*#).*/, '$1')}${name} `, - valid: false - }) - ) - }) - } + this.items.push( + Item({ + title: project.name, + subtitle: `Move task to ${project.name}`, + autocomplete: `${query.replace(/(^.*#).*/, '$1')}${name} `, + valid: false + }) + ) + }) } -) +}) diff --git a/src/todoist/query.ts b/src/todoist/query.ts index 77a4618f..f2b63798 100644 --- a/src/todoist/query.ts +++ b/src/todoist/query.ts @@ -1,55 +1,60 @@ -import { getSetting } from '@/project'; -import { LabelAdapter, LabelList, parser, ProjectAdapter, ProjectList, Task, TaskList } from '@/todoist'; -import { Item, List } from '@/workflow'; -import compose from 'stampit'; +import { getSetting } from '@/project' +import { + LabelAdapter, + LabelList, + parser, + ProjectAdapter, + ProjectList, + Task, + TaskList +} from '@/todoist' +import { Item, List } from '@/workflow' +import compose from 'stampit' /** @hidden */ function createTask(parsed: todoist.Parsed, locale: string) { - let task = Task(parsed) - let taskList = TaskList({ tasks: [task], action: 'CREATE', locale }) + const task = Task(parsed) + const taskList = TaskList({ tasks: [task], action: 'CREATE', locale }) return taskList.write() } /** @hidden */ async function showProjects(query: string) { - let project = query.replace(/^.*#/, '').replace(/\[|\]/g, '') - let projects = await ProjectAdapter({ token: getSetting('token') }).query(project, 'name') + const project = query.replace(/^.*#/, '').replace(/\[|\]/g, '') + const projects = await ProjectAdapter({ token: getSetting('token') }).query(project, 'name') return ProjectList({ projects, query }).write() } /** @hidden */ async function showLabels(query: string) { - let label = query.replace(/^.*@/, '') - let labels = await LabelAdapter({ token: getSetting('token') }).query(label, 'name') + const label = query.replace(/^.*@/, '') + const labels = await LabelAdapter({ token: getSetting('token') }).query(label, 'name') return LabelList({ labels, query }).write() } /** @hidden */ function showPriorities(query: string) { - let priority = query.replace(/^.*?!!/, '').replace(/^.*?p/, '') - let priorityNames: { [index: string]: string } = { + const priority = query.replace(/^.*?!!/, '').replace(/^.*?p/, '') + const priorityNames: { [index: string]: string } = { '1': 'urgent', '2': 'high', '3': 'medium', '4': 'low' } - const Priority: workflow.PriorityFactory = compose( - Item, - { - init(this: workflow.ItemInstance, title: number) { - this.title = `${title}` - this.subtitle = `Set priority to ${priorityNames[`${title}`]}` - this.autocomplete = `${query - .replace(/(^.*!!)[1-4]$/, '$1') - .replace(/(^.*?p)[1-4]$/, '$1')}${title} ` - this.valid = false - } + const Priority: workflow.PriorityFactory = compose(Item, { + init(this: workflow.ItemInstance, title: number) { + this.title = `${title}` + this.subtitle = `Set priority to ${priorityNames[`${title}`]}` + this.autocomplete = `${query + .replace(/(^.*!!)[1-4]$/, '$1') + .replace(/(^.*?p)[1-4]$/, '$1')}${title} ` + this.valid = false } - ) + }) if (+priority >= 1 && +priority <= 4) { return List({ items: [Priority(+priority)] }).write() diff --git a/src/todoist/rest-api-v8.ts b/src/todoist/rest-api-v8.ts index d997b01d..9cc74f11 100644 --- a/src/todoist/rest-api-v8.ts +++ b/src/todoist/rest-api-v8.ts @@ -1,12 +1,12 @@ -import { cache } from '@/project'; -import { Label, Project, Task } from '@/todoist'; -import { uuid } from '@/workflow'; -import { filter } from 'fuzzaldrin'; -import got from 'got'; -import find from 'lodash.find'; -import unionBy from 'lodash.unionby'; -import compose from 'stampit'; -import { TODOIST_API_URI } from '@/project/references'; +import { cache } from '@/project' +import { Label, Project, Task } from '@/todoist' +import { uuid } from '@/workflow' +import { filter } from 'fuzzaldrin' +import got from 'got' +import find from 'lodash.find' +import unionBy from 'lodash.unionby' +import compose from 'stampit' +import { TODOIST_API_URI } from '@/project/references' /** @hidden */ const Adapter: todoist.AdapterFactory = compose({ @@ -48,11 +48,7 @@ const Adapter: todoist.AdapterFactory = compose({ * @param {string} [key='content'] * @returns {Promise} */ - async query( - this: todoist.TaskAdapterInstance, - query: string, - key: string = 'content' - ): Promise { + async query(this: todoist.TaskAdapterInstance, query: string, key = 'content'): Promise { if (!query) return this.findAll() return filter(await this.findAll(), query, { key }) @@ -92,209 +88,199 @@ const Adapter: todoist.AdapterFactory = compose({ }) /** @hidden */ -export const ProjectAdapter: todoist.ProjectAdapterFactory = compose( - Adapter, - { - init(this: todoist.ProjectAdapterInstance) { - this.type = 'project' - }, +export const ProjectAdapter: todoist.ProjectAdapterFactory = compose(Adapter, { + init(this: todoist.ProjectAdapterInstance) { + this.type = 'project' + }, - methods: { - /** - * Find a project by it's id. - * - * @param {number} id - * @returns A project instance - */ - async find(this: todoist.ProjectAdapterInstance, id: number): Promise { - // @ts-ignore: cache type definition is incorrect - let cachedProjects: Project[] = cache.get('projects') || [] - let project = find(cachedProjects, ['id', id]) + methods: { + /** + * Find a project by it's id. + * + * @param {number} id + * @returns A project instance + */ + async find(this: todoist.ProjectAdapterInstance, id: number): Promise { + // @ts-ignore: cache type definition is incorrect + const cachedProjects: Project[] = cache.get('projects') || [] + const project = find(cachedProjects, ['id', id]) - if (project) return project + if (project) return project - let { body } = await this.client.get(`projects/${id}`) + const { body } = await this.client.get(`projects/${id}`) - cachedProjects.push(body) - cache.set('projects', unionBy(cachedProjects, 'id')) + cachedProjects.push(body) + cache.set('projects', unionBy(cachedProjects, 'id')) - return Project(body) - }, + return Project(body) + }, - /** - * Get all projects - * - * @returns All projects - */ - async findAll(this: todoist.ProjectAdapterInstance): Promise { - // @ts-ignore: cache type definition is incorrect - let cachedProjects: Project[] = cache.get('projects') || [] + /** + * Get all projects + * + * @returns All projects + */ + async findAll(this: todoist.ProjectAdapterInstance): Promise { + // @ts-ignore: cache type definition is incorrect + const cachedProjects: Project[] = cache.get('projects') || [] - if (cachedProjects.length > 0) return cachedProjects + if (cachedProjects.length > 0) return cachedProjects - let { body } = await this.client.get('projects') + const { body } = await this.client.get('projects') - let projects: todoist.Project[] = body.map((project: todoist.Project) => { - return Project(project) - }) + const projects: todoist.Project[] = body.map((project: todoist.Project) => { + return Project(project) + }) - cache.set('projects', projects) + cache.set('projects', projects) - return projects - } + return projects } } -) +}) /** @hidden */ -export const LabelAdapter: todoist.LabelAdapterFactory = compose( - Adapter, - { - init(this: todoist.LabelAdapterInstance) { - this.type = 'label' +export const LabelAdapter: todoist.LabelAdapterFactory = compose(Adapter, { + init(this: todoist.LabelAdapterInstance) { + this.type = 'label' + }, + + methods: { + /** + * Find a label by it's id. + * + * @param {number} id + * @returns A label instance + */ + async find(this: todoist.LabelAdapterInstance, id: number): Promise { + // @ts-ignore: cache type definition is incorrect + const cachedLabels: Label[] = cache.get('labels') || [] + const label = find(cachedLabels, ['id', id]) + + if (label) return label + + const { body } = await this.client.get(`labels/${id}`) + + cachedLabels.push(body) + cache.set('labels', unionBy(cachedLabels, 'id')) + + return Label(body) }, - methods: { - /** - * Find a label by it's id. - * - * @param {number} id - * @returns A label instance - */ - async find(this: todoist.LabelAdapterInstance, id: number): Promise { - // @ts-ignore: cache type definition is incorrect - let cachedLabels: Label[] = cache.get('labels') || [] - let label = find(cachedLabels, ['id', id]) - - if (label) return label - - let { body } = await this.client.get(`labels/${id}`) - - cachedLabels.push(body) - cache.set('labels', unionBy(cachedLabels, 'id')) - - return Label(body) - }, - - /** - * Get all labels - * - * @returns All labels - */ - async findAll(this: todoist.LabelAdapterInstance): Promise { - // @ts-ignore: cache type definition is incorrect - let cachedLabels: Label[] = cache.get('labels') || [] - - if (cachedLabels.length > 0) return cachedLabels - - let { body } = await this.client.get('labels') - let labels: todoist.Label[] = body.map((label: todoist.Label) => { - return Label(label) - }) + /** + * Get all labels + * + * @returns All labels + */ + async findAll(this: todoist.LabelAdapterInstance): Promise { + // @ts-ignore: cache type definition is incorrect + const cachedLabels: Label[] = cache.get('labels') || [] - cache.set('labels', labels) + if (cachedLabels.length > 0) return cachedLabels - return labels - } + const { body } = await this.client.get('labels') + const labels: todoist.Label[] = body.map((label: todoist.Label) => { + return Label(label) + }) + + cache.set('labels', labels) + + return labels } } -) +}) /** @hidden */ -export const TaskAdapter: todoist.TaskAdapterFactory = compose( - Adapter, - { - init(this: todoist.TaskAdapterInstance) { - this.type = 'task' - }, +export const TaskAdapter: todoist.TaskAdapterFactory = compose(Adapter, { + init(this: todoist.TaskAdapterInstance) { + this.type = 'task' + }, - methods: { - /** - * Find a task by it's id. - * - * @param {number} id - * @returns A task instance - */ - async find(this: todoist.TaskAdapterInstance, id: number): Promise { - // @ts-ignore: cache type definition is incorrect - let cachedTasks: todoist.Task[] = cache.get('tasks') || [] - let task = find(cachedTasks, ['id', id]) + methods: { + /** + * Find a task by it's id. + * + * @param {number} id + * @returns A task instance + */ + async find(this: todoist.TaskAdapterInstance, id: number): Promise { + // @ts-ignore: cache type definition is incorrect + const cachedTasks: todoist.Task[] = cache.get('tasks') || [] + const task = find(cachedTasks, ['id', id]) - if (task) return task + if (task) return task - let { body } = await this.client.get(`tasks/${id}`) + const { body } = await this.client.get(`tasks/${id}`) - cachedTasks.push(body) - cache.set('tasks', unionBy(cachedTasks, 'id')) + cachedTasks.push(body) + cache.set('tasks', unionBy(cachedTasks, 'id')) - return Task(body) - }, + return Task(body) + }, - /** - * Get all tasks - * - * @returns All tasks - */ - async findAll(this: todoist.TaskAdapterInstance): Promise { - // @ts-ignore: cache type definition is incorrect - let cachedTasks: todoist.Task[] = cache.get('tasks') || [] + /** + * Get all tasks + * + * @returns All tasks + */ + async findAll(this: todoist.TaskAdapterInstance): Promise { + // @ts-ignore: cache type definition is incorrect + const cachedTasks: todoist.Task[] = cache.get('tasks') || [] - if (cachedTasks.length > 0) return cachedTasks + if (cachedTasks.length > 0) return cachedTasks - let { body } = await this.client.get('tasks') + const { body } = await this.client.get('tasks') - // Cache label and projects - await ProjectAdapter({ token: this.token }).findAll() - await LabelAdapter({ token: this.token }).findAll() + // Cache label and projects + await ProjectAdapter({ token: this.token }).findAll() + await LabelAdapter({ token: this.token }).findAll() - let mapped = body.map(async (task: todoist.Task) => { - return Task(await this.getRelationships(task)) // tslint:disable-line - }) + const mapped = body.map(async (task: todoist.Task) => { + return Task(await this.getRelationships(task)) // tslint:disable-line + }) - return Promise.all(mapped).then(([...tasks]: any) => { - cache.set('tasks', tasks) + return Promise.all(mapped).then(([...tasks]: any) => { + cache.set('tasks', tasks) - return tasks + return tasks + }) + }, + + /** + * API call to close but not delete a task. + * + * @param {number} id + * @returns {Promise} + */ + async close(this: todoist.AdapterInstance, id: number): Promise { + return this.client.post(`${this.type}s/${id}/close`) + }, + + /** + * Retrieve a tasks labels an projects by id. + * + * @param {todoist.TaskAdapterInstance} this + * @param {todoist.Task} task + * @returns {Promise} + */ + async getRelationships( + this: todoist.TaskAdapterInstance, + task: todoist.Task + ): Promise { + // Don't mutate task object + const t = Object.assign({}, { labels: [] }, task) + const projectAdapter = ProjectAdapter({ token: this.token }) + const labelAdapter = LabelAdapter({ token: this.token }) + + t.project = await projectAdapter.find(task.project_id) + + if (t.label_ids) { + t.label_ids.forEach(async (labelId: number) => { + t.labels.push(await labelAdapter.find(labelId)) }) - }, - - /** - * API call to close but not delete a task. - * - * @param {number} id - * @returns {Promise} - */ - async close(this: todoist.AdapterInstance, id: number): Promise { - return this.client.post(`${this.type}s/${id}/close`) - }, - - - /** - * Retrieve a tasks labels an projects by id. - * - * @param {todoist.TaskAdapterInstance} this - * @param {todoist.Task} task - * @returns {Promise} - */ - async getRelationships( - this: todoist.TaskAdapterInstance, - task: todoist.Task - ): Promise { - // Don't mutate task object - let t = Object.assign({}, { labels: [] }, task) - let projectAdapter = ProjectAdapter({ token: this.token }) - let labelAdapter = LabelAdapter({ token: this.token }) - - t.project = await projectAdapter.find(task.project_id) - - if (t.label_ids) { - t.label_ids.forEach(async (labelId: number) => { - t.labels.push(await labelAdapter.find(labelId)) - }) - } - - return t } + + return t } } -) +}) diff --git a/src/todoist/task.ts b/src/todoist/task.ts index 2e58bb27..d96e691e 100644 --- a/src/todoist/task.ts +++ b/src/todoist/task.ts @@ -35,73 +35,70 @@ export const Task: todoist.TaskFactory = compose({ }) /** @hidden */ -export const TaskList: todoist.TaskListFactory = compose( - List, - { - init( - this: todoist.TaskListInstance, - { - tasks = [], - action = 'COMPLETE', - locale = 'en' - }: { tasks: todoist.Task[]; action: string; locale: todoist.locale } - ) { - tasks.forEach((task: todoist.Task) => { - const { content, project, labels = [], priority, due, due_string } = task - let view = View() - let name = (project && project.name) || '' - let date: string +export const TaskList: todoist.TaskListFactory = compose(List, { + init( + this: todoist.TaskListInstance, + { + tasks = [], + action = 'COMPLETE', + locale = 'en' + }: { tasks: todoist.Task[]; action: string; locale: todoist.locale } + ) { + tasks.forEach((task: todoist.Task) => { + const { content, project, labels = [], priority, due, due_string } = task + let view = View() + const name = (project && project.name) || '' + let date: string - if (due && due.date) { - date = due.date - } + if (due && due.date) { + date = due.date + } - if (due && due.datetime) { - date = due.datetime - } + if (due && due.datetime) { + date = due.datetime + } - let item = Item({ - arg: task, - title: `${action}: ${content}`, - subtitle: view.template( - ({ upperCase, ws, when }) => { - // Project name - let subtitle = `${when(name, upperCase(name), 'INBOX')}` + const item = Item({ + arg: task, + title: `${action}: ${content}`, + subtitle: view.template(({ upperCase, ws, when }) => { + // Project name + let subtitle = `${when(name, upperCase(name), 'INBOX')}` - // Label - subtitle += `${when( - labels.length > 0, - `${ws(10)}\uFF20 ${labels.map(label => label.name)}`, - '' - )}` + // Label + subtitle += `${when( + labels.length > 0, + `${ws(10)}\uFF20 ${labels.map(label => label.name)}`, + '' + )}` - // Priority - subtitle += `${when( - priority && priority > 1, - `${ws(10)}\u203C ${priority && 5 - priority}`, - '' - )}` + // Priority + subtitle += `${when( + priority && priority > 1, + `${ws(10)}\u203C ${priority && 5 - priority}`, + '' + )}` - // Due date (in local language) - if (date) { - subtitle += `${when( - date, - `${ws(10)}\u29D6 ${formatDistance(parseISO(date), new Date(), { - addSuffix: true, - locale: LOCALES[locale] - })}`, - '' - )}`} + // Due date (in local language) + if (date) { + subtitle += `${when( + date, + `${ws(10)}\u29D6 ${formatDistance(parseISO(date), new Date(), { + addSuffix: true, + locale: LOCALES[locale] + })}`, + '' + )}`} + } - // Alternative due date - subtitle += `${when(due_string, `${ws(10)}\u29D6 ${due_string}`, '')}` + // Alternative due date + subtitle += `${when(due_string, `${ws(10)}\u29D6 ${due_string}`, '')}` - return subtitle - }) + return subtitle }) - - this.items.push(item) }) - } + + this.items.push(item) + }) } -) +}) diff --git a/src/workflow/item.ts b/src/workflow/item.ts index 2159b93b..afe15a2b 100644 --- a/src/workflow/item.ts +++ b/src/workflow/item.ts @@ -1,50 +1,47 @@ -import { Writable } from '@/workflow'; -import md5 from 'md5'; -import compose from 'stampit'; +import { Writable } from '@/workflow' +import md5 from 'md5' +import compose from 'stampit' /** * A workflow item. * * @hidden */ -export const Item: workflow.ItemFactory = compose( - Writable, - { - /** - * @constructor - * @param {workflow.Item} item - */ - init( - this: workflow.ItemInstance, - { - uid = void 0, - title = '', - subtitle = void 0, - icon = { path: 'icon.png' }, - arg = void 0, - type = 'default', - valid = true, - autocomplete = void 0, - match = void 0, - mod = void 0, - text = void 0, - quicklookurl = void 0 - } - ) { - Object.assign(this, { - uid: uid || md5(this.title + this.subtitle), - title, - subtitle, - icon, - arg: typeof 'object' ? JSON.stringify(arg) : arg, - type, - valid, - autocomplete, - match, - mod, - text, - quicklookurl - }) +export const Item: workflow.ItemFactory = compose(Writable, { + /** + * @constructor + * @param {workflow.Item} item + */ + init( + this: workflow.ItemInstance, + { + uid = void 0, + title = '', + subtitle = void 0, + icon = { path: 'icon.png' }, + arg = void 0, + type = 'default', + valid = true, + autocomplete = void 0, + match = void 0, + mod = void 0, + text = void 0, + quicklookurl = void 0 } + ) { + Object.assign(this, { + uid: uid || md5(this.title + this.subtitle), + title, + subtitle, + icon, + arg: typeof 'object' ? JSON.stringify(arg) : arg, + type, + valid, + autocomplete, + match, + mod, + text, + quicklookurl + }) } -) +}) diff --git a/src/workflow/list.ts b/src/workflow/list.ts index 8e34d3a5..930f94dc 100644 --- a/src/workflow/list.ts +++ b/src/workflow/list.ts @@ -1,16 +1,13 @@ -import { Writable } from '@/workflow'; -import compose from 'stampit'; +import { Writable } from '@/workflow' +import compose from 'stampit' /** @hidden */ -export const List: workflow.ListFactory = compose( - Writable, - { - /** - * @constructor - * @param {workflow.ListInstance} list - */ - init(this: workflow.ListInstance, { items = [] }: { items: workflow.Item[] | undefined }) { - this.items = items - } +export const List: workflow.ListFactory = compose(Writable, { + /** + * @constructor + * @param {workflow.ListInstance} list + */ + init(this: workflow.ListInstance, { items = [] }: { items: workflow.Item[] | undefined }) { + this.items = items } -) +})