30 Days of Algorithms in JavaScript

algorithms illustration

I recently decided to re-enter the “real world” after freelancing for three years. My very first interview was at InVision. It did not go well, to say the least. The coding test focused on common algorithms in JavaScript, and I bombed. After receiving the polite rejection email, I asked the recruiter for feedback from the hiring team so I could improve.

Continue reading

Create JavaScript models from JSON

For the past several months I have been working on a new Adobe Illustrator extension to make the functionality of IconJar available as a panel. In order to manipulate the IconJar data – which is stored in JSON, I needed to create “Plain Old JavaScript Objects” (POJsO?) with getters and setters. Rather than type out the same lines of code over-and-over, I decided to create a command-line utility to quickly create the JavaScript classes for me.

Continue reading

JSON to JavaScript Model

json-to-js-model

Quickly and easily generate JavaScript models (POJsO –
Plain Old JavaScript Objects) from a JSON object with getters & setters, data type validation, including single
item classes and collections.

Usage:

json-to-js-model can be used as an import in
a Node project or as a command-line utility. The tool takes a JSON description file of key -> value pairs and
generates a JavaScript model with getters and setters.

Single Item example

{
  "identifier"   : "C60ED43C-FB42-4321-AAA4-2CD344CB2B91",
  "name"         : "girl-in-ballcap",
  "tags"         : "ballcap,girl,in",
  "file"         : "girl-in-ballcap.svg",
  "licence"      : "",
  "date"         : "2019-06-13 07:36:28",
  "width"        : 0,
  "height"       : 0,
  "parent"       : "F19A7973-0CB3-4751-B74F-E2AF0F9B2AF4",
  "type"         : 0,
  "unicode"      : "",
  "__primaryKey" : "identifier",
  "__parent"     : "parent",
  "__type"       : "item",
  "__className"  : "Icon"
}

Collection example

{
  "identifier"   : "E50143AB-F36E-4CA9-852A-46E83B1C3928",
  "name"         : "Set Three",
  "parent"       : "B4FF6C41-534D-40CE-B5FF-6BFBB21E5471",
  "date"         : "2020-01-22 20:48:51",
  "licence"      : "",
  "sort"         : 2,
  "enabled"      : true,
  "children"     : [],
  "__primaryKey" : "identifier",
  "__parent"     : "parent",
  "__children"   : "children",
  "__type"       : "collection",
  "__className"  : "IconSet"
}

Usage

Meta Properties

json-to-js-model can figure out which
properties your class should have, but it needs some help understanding how your classes work together. For this
reason, you will need to add some very basic meta properties to your JSON. Properties that begin with
two underscores __propertyName are private and used by json-to-js-model to determine how
to prepare or link the classes. There are only five (5) pre-defined private properties:

  • __primaryKey
  • __parent
  • __children
  • __type
  • __className

You can use your actual JSON data to create the classes
or you can code up schemas that are identical to your JSON data but include only sample data.

__primaryKey

Specifies the name of the primary key field such as ID,
identifier, GUID, etc. This is not the value of the field, it is the name of the field.

__parent

Specifies the name of the parent field. The parent
field connects items and sets (collections) via a primary key. The __parent should be set on the single
item and should point to the __primaryKey of the collection.

__children

Specifies the field on the collection which will
contain the array of single items. This is the name of the field, not the value of the field.
__children should be set on the collection definition, not the item unless items can also be
collections such as in a multi-level hierarchy.

__type

The type can be either item or
collection and tells json-to-js-model which JavaScript class template to use.

__className

Specifies the name of the class to be created.

Specifying Data Types

In most cases, json-to-js-model can figure
out what the intended type is, including dates (using Date.parse()). If you encounter an error in
json-to-js-model‘s type detection, you can explicitly declare the type by appending it to the field
(property) name with double colons firstName::string or birthday::date or
age::number. json-to-js-model will honor the declared type over all other considerations.
It will also strip the type declaration from the final property name so you won’t end up with
firstName::string in your JS class.

DO NOT add type delcarations to meta
properties (those with two underscores at the beginning of the name).

NB : On the roadmap I plan to add
complex type validations like url, email, etc. The best way to learn how to use the
package is probably looking at the files in test, in particular the definitions. The
markup is very simple and easy-to-understand. It’s just JSON with a few extra properties to tell the parser how you
intend for your classes to be connected.

Example Type
Declaraction

{
  "identifier::string"   : "C60ED43C-FB42-4321-AAA4-2CD344CB2B91",
  "name::string"         : "girl-in-ballcap",
  "tags::array"          : ["girl", "ball cap", "baseball"],
  "file::string"         : "girl-in-ballcap.svg",
  "licence::string"      : "",
  "modified::date"       : "2019-06-13 07:36:28",
  "width::number"        : 0,
  "height:number"        : 0,

  "__primaryKey" : "identifier",
  "__parent"     : "parent",
  "__type"       : "item",
  "__className"  : "Icon"
}

Example
command-line usage:

Notice that you can pass as many file paths as you
want. The file paths point to the JSON description
files for the classes to be created.

node cli.js ./path/to/item.json ./path/to/collection.json

Example import usage:

const jsonToJsModel = require('../index');

console.log(new jsonToJsModel('Icon.json', './output').getOutput());
console.log(new jsonToJsModel('IconSet.json', './output').getOutput());

Why is this not written in modern JavaScript?

That is a legitimate question and I wish I
could write it all in ES6, but I primarily built this tool for my own needs building Adobe CEP (Common
Extensibility Platform) extensions for Adobe Illustrator. Since CEP is an older technology, it does not
always support ES6. Each Adobe product has a different extension API. Some use UXP (XD, for example) and
others (most) use their own implementation of CEP which is based on CEF (Chromium Extension Framework).

So in order to be able to use the tool for my work
building Illustrator and Photoshop extensions, I had to generate ES5 classes instead of ES6. This is, however,
alpha code and the plan is to update the package itself to use only ES6 and to generate both ES6 and ES5
output.

Known Issues

  • A fair amount of code is duplicated in paird
    collection and item classes.
  • Methods like generateUUID() do not
    belong in the resulting class. They were added to avoid dependencies. The solution is to output a utility file
    with the classes that will allow developers to either use the provided utility file, or roll their own. Another
    option is to use existing NPM packages for this functionality whenever possible.Everything is a trade-off and
    for now self-encapsulation is the best, simplest solution.
  • Need to remove unnecessary dev files from
    package.

Roadmap

  • Add complex type validations for things like
    email, url, etc.
  • Add unit tests for this package and for
    resulting classes (some basic testing is in test.js already)
  • Add recursive parsing to allow class hierarchies
    to be defined in a single JSON file.
971 Words

Contact Sheet

A JavaScript extension for Adobe Illustrator to create a contact sheet from a folder of SVG files. The script allows you select a folder of SVG files and imports and arranges them in a grid pattern as a contact sheet. You can specify the page width and height, number of columns and rows, and the scale of the imported files.

Donations

I am a freelance developer and your donations help me continue to create free resources. You can donate to this project using the button below. Every bit helps.

Installation

To use this script, you will need to copy the entire Contact Sheet folder to your Illustrator scripts folder, then restart Illustrator. Follow the steps below to install.

  1. Unzip the Contact Sheet ZIP archive.
  2. Copy the Contact Sheet folder to Adobe Illlustrator/Presets/{language}/Scripts/ where {language} is your chosen language. For example, if you have a US version of Illustrator this will be en_US.
  3. Restart Illustrator
  4. Once Illustrator restarts, verify that the script was installed by going to File > Scripts > Contact Sheet

Usage

  1. Go to File > Scripts > Contact Sheet to launch the script. You will see a dialog like the one below.
    Contact Sheet preferences dialog
  2. Contact Sheet creates a single artboard, imports a folder of SVG files you specify, and arranges them in a grid. Using the dialog inputs, specify the page width and height, the number of columns and rows, the scale of the imports (from 1 to 100 – do not include the percent sign). Once you have the values you want, you can click the Save Preset button to save these settings for future use.
  3. Click the Choose Folder button to select your folder of SVG files (NOTE: Contact Sheet was created to create previews of icon sets so for now it only works with SVG files).
  4. Double-check your settings and click Ok. Contact Sheet will display a progress dialog to let you know how many files are left to import. Once the files are imported and arranged, the file will be saved to the name you specified.
  5. If you checked the Logging? checkbox, you can view the log file in /your-home-folder/ai-contact-sheet where ~/. For example, on a Mac the folder can be found at /Users/yourname/ai-contact-sheet/ (or ~/ai-contact-sheet/ for shorthand). The preset configuration files can also be found in this location.Contact Sheet progress bar

Custom Configuration

NOTE : changing the default configuration can break the Contact Sheet utility. Proceed with caution.

You can change many of the default settings such as the location of the presets and log files, Illustrator version compatibility, etc., by editing the config.js file in the download.

Credits

You are free to use, modify, and distribute this script as you see fit as long as you maintain the copyright notices in the original source files. A link to the Atomic Lotus website would be appreciated as well. You must also extend the same license to users of your code. This is not to say that your original code must be open source, but the code from this project must remain free and open forever.

Contact Sheet by :

Scott Lewis <scott@atomiclotus.net>
https://atomiclotus.net

Disclaimer of Liability

This script is offered AS-IS without any warranty or guarantees of any kind. You use this script completely at your own risk and under no circumstances will the developer and/or distributor of this script be held liable for damages of any kind including loss of data or damage to hardware or software. If you do not agree to these terms, do not use this script.

426 Words

Adobe Illustrator Contact Sheet JSX Plugin

I don’t like performing tedious, time-consuming tasks, especially when those tasks are non-revenue generating, which means they take time away from things I could be doing to increase revenue. The most time-consuming and tedious task I have to perform over-and-over is creating contacts sheet previews of my icons. The problem is that every marketplace has different requirements for preview image sizes and so a new contact sheet has to be created for each marketplace.
Continue reading

Ai Sessions Adobe Illustrator JSX Extension

When I’m working on my icon designs, I often have 4-5 Adobe Illustrator documents open at a time during a work session. Since icon design involves creating large collections of tens, hundreds, or even thousands of icons, I have them broken up into multiple files but find I need to copy a lot of icons between files.

Continue reading

Remove an Element from an HTML String with jQuery

While working on a project for work today, I encountered a problem that I apparently have never encountered before. What I thought was a very simple function call in jQuery turned out to be a bit more complicated. I needed to removed an HTML element from a string representation of an HTML snippet. jQuery doesn’t quite behave the way I expected and I had trouble finding a solution.

Continue reading

jQuery Plugin to Toggle Default Field Value on Focus and Blur

I can’t begin to count the number of times I have coded the same search field with the default value “Search…” in it and so that when the field receives focus, the text is cleared but magically reappears when the field blurs. Every time I code it I know I should save that snippet of code somewhere but it is always faster to just write it anew each time. Well, no more. I finally got around to writing jQuery plugin to allow me to add the focus/blur default value toggle to any field. I have very creatively named the plugin ‘Defaultify’.

Continue reading

Simple jQuery Plugin Example

In this article, I will teach you how to develop a standard jQuery plugin. I will keep things as simple as possible and will only build a very trivial plugin, but through this example I will cover all of the important aspects of jQuery plugin development.

Continue reading