From e1d33ca1e4b17a0ace02f14941eabac34b65019c Mon Sep 17 00:00:00 2001 From: Shaunak Kishore Date: Mon, 28 Sep 2015 16:08:47 -0400 Subject: [PATCH] Clean up Glyphs model methods --- lib/glyphs.js | 48 +++++++++++++++++++++++------------------ server/glyphs.js | 56 ------------------------------------------------ 2 files changed, 27 insertions(+), 77 deletions(-) delete mode 100644 server/glyphs.js diff --git a/lib/glyphs.js b/lib/glyphs.js index e9f1c976..2e96a785 100644 --- a/lib/glyphs.js +++ b/lib/glyphs.js @@ -2,27 +2,33 @@ this.Glyphs = new Mongo.Collection('glyphs'); -Glyphs.findGlyphsForRadical = function(radical) { - if (radical) { - return Glyphs.find({'index.radical': radical}); - } - return Glyphs.find({'index.strokes': 0}); +Glyphs.get = (query) => Glyphs.findOne(query); + +Glyphs.getNext = (glyph) => { + const codepoint = glyph ? glyph.codepoint : undefined; + const next = Glyphs.findOne( + {codepoint: {$gt: codepoint}}, {sort: {codepoint: 1}}); + return next ? next : Glyphs.findOne({}, {sort: {codepoint: 1}}); } -Glyphs.get_svg_path = function(glyph) { - var terms = []; - for (var i = 0; i < glyph.path.length; i++) { - var segment = glyph.path[i]; - assert('LMQZ'.indexOf(segment.type) >= 0, segment.type); - terms.push(segment.type); - if (segment.x1 !== undefined) { - terms.push(segment.x1); - terms.push(segment.y1); - } - if (segment.x !== undefined) { - terms.push(segment.x); - terms.push(segment.y); - } - } - return terms.join(' '); +Glyphs.getPrevious = (glyph) => { + const codepoint = glyph ? glyph.codepoint : undefined; + const previous = Glyphs.findOne( + {codepoint: {$lt: codepoint}}, {sort: {codepoint: -1}}); + return previous ? previous : Glyphs.findOne({}, {sort: {codepoint: -1}}); +} + +Glyphs.save = (glyph) => { + check(glyph.character, String); + assert(glyph.character.length === 1); + Glyphs.upsert({character: glyph.character}, glyph); +} + +// Register the methods above on the server so they are available to the client. +if (Meteor.isServer) { + const methods = {}; + const method_names = ['get', 'getNext', 'getPrevious', 'save']; + method_names.map((name) => methods[`${name}Glyph`] = Glyphs[name]); + methods.saveGlyphs = (glyphs) => glyphs.map(Glyphs.save); + Meteor.methods(methods); } diff --git a/server/glyphs.js b/server/glyphs.js deleted file mode 100644 index 9dd8b826..00000000 --- a/server/glyphs.js +++ /dev/null @@ -1,56 +0,0 @@ -"use strict"; - -function save_glyph(glyph) { - check(glyph.name, String); - var saved_glyph = _.extend({}, glyph); - if (saved_glyph.render !== undefined) { - saved_glyph.derived = { - errors: saved_glyph.render.log.filter(function(pair) { - return pair[0] !== 'success'; - }), - strokes: saved_glyph.render.strokes, - }; - } - delete saved_glyph.render; - Glyphs.upsert({name: glyph.name}, saved_glyph); - return glyph; -} - -Meteor.methods({ - get_glyph: function(character) { - return Glyphs.findOne({character: character}); - }, - get_next_glyph: function(glyph) { - var name = glyph ? glyph.name : undefined; - var next = Glyphs.findOne({name: {$gt: name}}, {sort: {name: 1}}); - return next ? next : Glyphs.findOne({}, {sort: {name: 1}}); - }, - get_next_glyph_skip_verified: function(glyph) { - var name = glyph ? glyph.name : undefined; - var next = Glyphs.findOne( - {name: {$gt: name}, 'manual.verified': {$ne: true}}, {sort: {name: 1}}); - return next ? next : Glyphs.findOne( - {'manual.verified': {$ne: true}}, {sort: {name: 1}}); - }, - get_previous_glyph: function(glyph) { - var name = glyph ? glyph.name : undefined; - var prev = Glyphs.findOne({name: {$lt: name}}, {sort: {name: -1}}); - return prev ? prev : Glyphs.findOne({}, {sort: {name: -1}}); - }, - get_previous_glyph_skip_verified: function(glyph) { - var name = glyph ? glyph.name : undefined; - var prev = Glyphs.findOne( - {name: {$lt: name}, 'manual.verified': {$ne: true}}, {sort: {name: -1}}); - return prev ? prev : Glyphs.findOne( - {'manual.verified': {$ne: true}}, {sort: {name: -1}}); - }, - get_fraction_verified: function() { - return Glyphs.find({'manual.verified': true}).count()/Glyphs.find().count(); - }, - save_glyph: save_glyph, - save_glyphs: function(glyphs) { - for (var i = 0; i < glyphs.length; i++) { - save_glyph(glyphs[i]); - } - }, -});