Skip to content

Commit

Permalink
Merge pull request #17 from jerrywdlee/bugfix-200329
Browse files Browse the repository at this point in the history
Bugfix 200329
  • Loading branch information
jerrywdlee committed Mar 29, 2020
2 parents 8eeb8b8 + b850996 commit 6dac54b
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 95 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ It also provides [TypeScript API](./index.d.ts) and all tests are written by Typ
## Requirements
- MacOS 10.10 and over
- Clipy v1.2.0 and over
- Node.js v8.9.0 and over
- Node.js v8.9.0 and < v12 ([Why?](https://github.com/realm/realm-js/issues/2418))

## Installation
Use the package manager [npm](https://www.npmjs.com/) to install.
Expand Down
4 changes: 3 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ declare class ClipyMate {
CPYSnippet?: {},
}>
readSnippets(orderByIndex?: boolean): Promise<ClipyMate.folder[]>
getFolder(folderId: string): Promise<ClipyMate.folder>
getSnippet(snippetId: string): Promise<ClipyMate.snippet>
upsertFolder(opt: ClipyMate.upsertFolderOpt): Promise<ClipyMate.folder>
upsertSnippet(opt: ClipyMate.upsertSnippetOpt, folderId?: string): Promise<ClipyMate.snippet>
destroyFolder(folderId: string, orderByIndex?: boolean): Promise<ClipyMate.folder>
Expand Down Expand Up @@ -75,7 +77,7 @@ declare namespace ClipyMate {
}
type folder = {
title: string,
snippets: snippet[],
snippets: snippet[] | upsertSnippetOpt[],
index: number,
identifier: string,
enable: boolean,
Expand Down
37 changes: 26 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class ClipyMate {
this.realm.schema.forEach(schema => {
const name = schema['name'];
if (keys.includes(name)) {
// console.log(name, schema);
schemas[name] = fmt.formSchema(schema);
}
});
Expand Down Expand Up @@ -162,20 +161,28 @@ class ClipyMate {
if (!realm || realm.isClosed) {
await this.init();
}
let folder = null;

let snippets = opt['snippets'];

const folderOpt = {
title: 'untitled folder',
identifier: uuidv4().toUpperCase(),
enable: true, ...opt, snippets: [],
}

let folder = await this.getFolder(folderOpt.identifier)
// if folder exsit, do not update snippets when touch the folder
if (folder) {
delete folderOpt.snippets;
}

if (!folderOpt['index']) {
folderOpt['index'] = await getIndex(this.CPYFolder);
}
realm.write(() => {
folder = realm.create('CPYFolder', folderOpt, true);
});

if (snippets && snippets.length > 0) {
const folderId = folderOpt.identifier;
for (const snpOpt of snippets) {
Expand All @@ -192,17 +199,15 @@ class ClipyMate {
}

if (opt['identifier']) {
const snippet = this.CPYSnippet
.filtered(`identifier == '${opt['identifier'].toUpperCase()}'`)[0];
const snippet = await this.getSnippet(opt['identifier']);
if (snippet) {
realm.write(() => {
realm.create('CPYSnippet', opt, true);
});
return snippet;
}
}
const folder = this.CPYFolder
.filtered(`identifier == '${folderId.toUpperCase()}'`)[0];
const folder = await this.getFolder(folderId);
if (!folder) {
throw Error(`No such folder!\nidentifier: ${folderId}`);
}
Expand All @@ -218,16 +223,15 @@ class ClipyMate {
realm.write(() => {
snippetIndex = folder.snippets.push(snippetOpt);
})
// console.log(snippetOpt);

return folder.snippets[snippetIndex - 1];
}

async destroyFolder(folderId, orderByIndex = true) {
if (!this.realm || this.realm.isClosed) {
await this.init();
}
const folder = this.CPYFolder
.filtered(`identifier == '${folderId.toUpperCase()}'`)[0];
const folder = await this.getFolder(folderId);
if (!folder) {
throw Error(`Cannot destroy folder\nidentifier: ${folderId}`);
}
Expand All @@ -241,8 +245,7 @@ class ClipyMate {
if (!this.realm || this.realm.isClosed) {
await this.init();
}
const snippet = this.CPYSnippet
.filtered(`identifier == '${snippetId.toUpperCase()}'`)[0];
const snippet = await this.getSnippet(snippetId);
if (!snippet) {
throw Error(`No such snippet!\nidentifier: ${snippetId}`);
}
Expand Down Expand Up @@ -277,6 +280,18 @@ class ClipyMate {
this.realm.close();
}

async getFolder(folderId) {
return this.CPYFolder.filtered(
`identifier == '${folderId.toUpperCase()}'`
)[0];
}

async getSnippet(snippetId) {
return this.CPYSnippet.filtered(
`identifier == '${snippetId.toUpperCase()}'`
)[0];
}

}

async function formListener(eventName, collection, changes, fn, rawCollection) {
Expand Down
43 changes: 26 additions & 17 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ module.exports = {
// All imported modules in your tests should be mocked automatically
// automock: false,

// Stop running tests after the first failure
// bail: false,
// Stop running tests after `n` failures
// bail: 0,

// Respect "browser" field in package.json when resolving modules
// browser: false,

// The directory where Jest should store its cached dependency information
// cacheDirectory: "/var/folders/5y/ykyy3sb91cnf5pnbtfsxdcy40000gn/T/jest_dx",
// cacheDirectory: "/private/var/folders/1r/npvfcq595hs4fvhwwwnjj73472pvds/T/jest_3xjbg9",

// Automatically clear mock calls and instances between every test
clearMocks: true,
Expand Down Expand Up @@ -40,12 +40,15 @@ module.exports = {
// ],

// An object that configures minimum threshold enforcement for coverage results
// coverageThreshold: null,
// coverageThreshold: undefined,

// A path to a custom dependency extractor
// dependencyExtractor: undefined,

// Make calling deprecated APIs throw helpful error messages
// errorOnDeprecated: false,

// Force coverage collection from ignored files usin a array of glob patterns
// Force coverage collection from ignored files using an array of glob patterns
// forceCoverageMatch: [],

// A path to a module which exports an async function that is triggered once before all test suites
Expand All @@ -61,19 +64,25 @@ module.exports = {
}
},

// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
// maxWorkers: "50%",

// An array of directory names to be searched recursively up from the requiring module's location
// moduleDirectories: [
// "node_modules"
// ],

// An array of file extensions your modules use
moduleFileExtensions: [
"js",
"json",
"jsx",
"ts",
"tsx",
"js"
"node"
],

// A map from regular expressions to module names that allow to stub out resources with a single module
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
// moduleNameMapper: {},

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
Expand All @@ -83,13 +92,13 @@ module.exports = {
// notify: false,

// An enum that specifies notification mode. Requires { notify: true }
// notifyMode: "always",
// notifyMode: "failure-change",

// A preset that is used as a base for Jest's configuration
// preset: null,
// preset: undefined,

// Run tests from one or more projects
// projects: null,
// projects: undefined,

// Use this configuration option to add custom reporters to Jest
// reporters: undefined,
Expand All @@ -101,7 +110,7 @@ module.exports = {
// resetModules: false,

// A path to a custom resolver
// resolver: null,
// resolver: undefined,

// Automatically restore mock state between every test
// restoreMocks: false,
Expand All @@ -120,8 +129,8 @@ module.exports = {
// The paths to modules that run some code to configure or set up the testing environment before each test
// setupFiles: [],

// The path to a module that runs some code to configure or set up the testing framework before each test
// setupTestFrameworkScriptFile: null,
// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],

// A list of paths to snapshot serializer modules Jest should use for snapshot testing
// snapshotSerializers: [],
Expand All @@ -145,11 +154,11 @@ module.exports = {
// "/node_modules/"
// ],

// The regexp pattern Jest uses to detect test files
// testRegex: "",
// The regexp pattern or array of patterns that Jest uses to detect test files
// testRegex: [],

// This option allows the use of a custom results processor
// testResultsProcessor: null,
// testResultsProcessor: undefined,

// This option allows use of a custom test runner
// testRunner: "jasmine2",
Expand All @@ -174,7 +183,7 @@ module.exports = {
// unmockedModulePathPatterns: undefined,

// Indicates whether each individual test should be reported during the run
// verbose: null,
// verbose: undefined,

// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
// watchPathIgnorePatterns: [],
Expand Down
44 changes: 1 addition & 43 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clipy-mate-core",
"version": "1.1.0",
"version": "1.2.0",
"description": "Core functions for Clipy Mate",
"repository": "https://github.com/jerrywdlee/clipy-mate-core.git",
"main": "index.js",
Expand Down Expand Up @@ -33,7 +33,6 @@
"coveralls": "^3.0.9",
"jest": "^25.1.0",
"ts-jest": "^25.2.1",
"ts-node": "^7.0.1",
"typescript": "^3.8.3"
}
}

0 comments on commit 6dac54b

Please sign in to comment.