Don’t Repeat Yourself — Leverage Javascript library in Swift.

Tonny
3 min readSep 3, 2020

In the early stage of iOS development, we may need some mocking data or generated data to feed UI. And there are some awesome JS libraries which we can leverage to speed up iOS development. In this story, I will take faker.js as an example to show you how to leverage JS code in Swift.

What is faker.js

faker.js — generate massive amounts of fake data in the browser and node.js

faker.js is a great collection of common data, such as country, user name, email, avatar, etc.

name: firstName, lastName, findName, jobTitle, gender, prefix, suffix, title, jobDescriptor, jobArea, jobTypeaddress: zipCode, zipCodeByState, city, cityPrefix, citySuffix, streetName, streetAddress, streetSuffix, streetPrefix, secondaryAddress, county, country, countryCode, state, stateAbbr, latitude, longitude, direction, cardinalDirection, ordinalDirection, nearbyGPSCoordinate, timeZoneimage: avatar, imageUrl, abstract, animals, business, cats, city, food, nightlife, fashion, people, nature, sports, technics, transport, dataUri, lorempixel, unsplash, lorempicsumphone: phoneNumber, phoneNumberFormat, phoneFormatsrandom: number, float, arrayElement, arrayElements, objectElement, uuid, boolean, word, words, image, locale, alpha, alphaNumeric, hexaDecimalcommerce: color, department, productName, price, productAdjective, productMaterial, product, productDescriptioncompany: suffixes, companyName, companySuffix, catchPhrase, bs, catchPhraseAdjective, catchPhraseDescriptor, catchPhraseNoun, bsAdjective, bsBuzz, bsNoundate: past, future, between, recent, soon, month, weekdayfinance: account, accountName, routingNumber, mask, amount, transactionType, currencyCode, currencyName, currencySymbol, bitcoinAddress, litecoinAddress, creditCardNumber, creditCardCVV, ethereumAddress, iban, bic, transactionDescriptioninternet: avatar, email, exampleEmail, userName, protocol, url, domainName, domainSuffix, domainWord, ip, ipv6, userAgent, color, mac, passwordlorem: word, words, sentence, slug, sentences, paragraph, paragraphs, text, lines...and more

In JS, you can use faker.js to generate random data.

//javascriptconst faker = require('faker');

const randomCountry = faker.address.country();
const randomEmail = faker.internet.email();

Enumeration case in Swift mapping to the function name in JS

To leverage it in Swift, the first thing is importing the js library into JSContext with JavaScriptCore framework. Because there are no window and global variables existed in JSContext, so we need to create a global variable with the current context.

And we use nested enumeration to keep namespace consistency with JS. The case represents the function name of faker JS.

How to generate data in Swift?

When we call the random computed property of enumeration case, it just delegates the job to faker JS through JSContext.

//swift
let randomCountry = Faker.Address.country.random

We fully embrace this JS library if we continued mapping additional enumerations to faker JS. Now with the generated data, we can free ourself to feed up the UI without waiting for the back-end API get ready.

--

--