Category: Javascript (page 2 of 3)

Sails.js count blueprint

I’ve been in need of a count blueprint for a while now, so I decided to just go ahead and make one. IF you want to use it, feel free. Here’s the code:

Documentation for everything

Hello fellow geeks!

Today I’d like to share a nice piece of software that has boosted my productivity by 200%. It’s a tool that allows you to download documentation from a lot of libraries, frameworks, programming / scripting languages, stack overflow, packagist and more. It’s called Dash (for mac) and Zeal docs for Windows / linux. For this post I will be talking about Dash specifically, but most of it also applies to zealdocs.

What it does

Like they say themselves:

Dash gives your Mac instant offline access to 150+ API documentation sets.

It integrates with the system (shortkey) and a lot of editors / other systems. As an example, in your editor, you can select some text (for instance, Angular’s $http) and push the shortkeys for Dash (defaults to cmd+shift+d), which will bring up the documentation on $http for angular.js. Are you writing php, and are you unsure on what strstr or stristr do? Select that, press the shortkeys and boom, documentation.

Like I said, it has documentation for a lot of things. Node.js, PHP, javascript, ruby, Zend Framework 2, Laravel, and much, much, much (I can keep going like this) much more.

It also supports Packagist and automatically builds documentation for all packages you wish to use, if there aren’t any. And it supports stackoverflow by downloading answered questions which you can search.

Download

It’s offline, fast, has everything in one place and doesn’t force you to wait for the browser. It’s just cool. Check it out:

Mac: Dash

Windows/Linux: Zeal docs

Javascript censoring text

Yesterday I released a new module. I thought about it for a bit and decided to write about it.
The reason for this, is that I want to elaborate the reason for building this module, and at the same time perhaps supply a use-case to you.
I’m firmly against censoring anyone or anything, so I hope this module, if at all, gets used for the good.

Why I built it

We’re running and providing some very high-traffic communications services, one of them being chat.
The chat doesn’t allow you to share contact information (for security purposes) and so we needed a way to both sensor occurrences, and be notified whenever it happened.
When receiving notifications we also wanted to see the offending matches highlighted, and for that reason, I’ve added the highlight functionality.

The module was built with performance (regex, rather complex can become a slowing factor on high traffic messaging systems) and ease of use in mind.
It’s not rocket science, so it doesn’t need a freakishly large and complicated engine.

And thus, behold, the censoring module was born.

First, some code

Code speaks more than… Me. So here’s an example:

What it does

The module is compact, but does what it should do very well. What it does:

  • Supply a default set of filters
  • Supply the possibility to censor words
  • Supply the possibility to highlight matches (found abuses)
  • Detect words, even if they’ve b33n 13373d…
  • or s.p.l-i*t up.

What it doesn’t

To make sure you don’t get the wrong idea, I want to clear up some (already) misconceptions.

  • It doesn’t censor a predefined set of words.
  • It doesn’t actually censor by default.
  • It doesn’t swap words.

Where can I find the module?

Github: https://github.com/SpoonX/Censoring

NPM: https://www.npmjs.org/package/censoring

Installation is simple:

npm install --save censoring

Q&A: sails.js enable jsonp

Question

This is a question I had myself. Why, if I call /foo?callback=bar do I not get a jsonp response?

Answer

Out of the box, sails doesn’t enable jsonp support. In order to enable this, you have to change config/blueprints.js and add jsonp: true.

Q&A: Sails.js won’t run through forever.js

Question

Since recent releases of sails.js, for some people it seems like sails won’t run using forever anymore.

On github, SuThaw asked:

I installed forever js with “npm install forever -g” and I run it like this “forever start app.js –prod”. when I open up my browser (http:127.0.0.1:1337), nothing shows up.

Answer

The answer, is a new “safety switch” that has been added to migrations. This safety switch protects you from accidentally erasing precious data from your database when changing the properties of models due to database errors. What happens, is that sails asks you a question, that requires user input to be answered and go away (continue the flow). Using forever, this never happens. The message sails gives you is:

Excuse my interruption, but it looks like this app does not have a project-wide “migrate” setting configured yet. (perhaps this is the first time you’re lifting it with models?)

And gives you the following options:

  1. safe – never auto-migrate my database(s). I will do it myself (by hand)
  2. alter – auto-migrate, but attempt to keep my existing data (experimental)
  3. drop – wipe/drop ALL my data and rebuild models every time I lift Sails

To fix this, all you have to do is add a default migrate option in config.models.js. You could, for instance, simply add safe:

Q&A: autoWatch false still pushes updates sails.js

Question

The autoWatch configuration option for blueprints seems to be causing some confusion every now and then. Specifically, the question is:

Why, after disabling autoWatch by setting it to false, do I still receive updates from pubsub?

Answer

Taken from the sails documentation, the option autoWatch allows you to specify:

Whether to run Model.watch() in the find and findOne blueprint actions. Can be overridden on a per-model basis.

The function Model.watch() watches for newly created records for that specific model; not updates. The subscribe method however, is responsible for updates.

Found here, you can see sails uses a different option for pubsub:

If you want to disable pubsub, you can read how to do that on disabling pubsub in sails.

Q&A: Disable pubsub sails.js

I love short, concise blog posts. Let this be one of them!

Question

After having had this question asked a couple of times, I decided it qualified for a dedicated blog post. This particular question has been asked on IRC, a while back on github and in a sense, as well on StackOverflow.

[10:29:53] RWOverdijk: do u know if there is a way to completely shut off the pubsub feature in blueprints?

Answer

In your project’s root, create a file called .sailsrc. Inside of this file, add the following:

Voila, you’ve disabled pubsub.

Javascript dependencies

The never ending journey.

As a lot of you might have had as well, I’ve had a lot of problems with managing my javascript dependencies. There are a lot of ways you can do it, but most of them either require a lot of work, or have too many dependencies to work with.

I’ve searched long and hard, trying to find the best solution. I’ve come across Require.js, browserify, grunt and many more, but these were the best solutions in the bunch. Here are some of my findings on these three.

Require.JS

Require.JS is different in that it’s mainly meant to do AMD loading. It allows you to do some cool things, and actually works quite well. The problems I have with require.js are as follows:

  • It forces you to wrap your code in a function call.
  • It forces another (quite large) dependency on you.
  • It’s difficult to add non-AMD dependencies.
  • When a non-AMD dependency requires the use of another non-AMD file, good luck.
  • When you want to add unit tests you’ll have to work around require.js.

It’s good, but not good enough for me.

Browserify

Browserify looks very appealing when you look at it for the first time. Using require calls in my front-end javascript!? Yes please! Unfortunately, after trying to work around it for quite a while, I’ve found a couple of pretty upsetting drawbacks:

  • It’s a bit fragile and doesn’t always do what you’d expect.
  • It makes generating code coverage a tad difficult (it doesn’t recognize the require calls).
  • It forces a dependency on you.

It’s better than require.js, but still not good enough.

Grunt (concat, usemin)

GruntJS, the task-running bamf. It’s amazing in that grunt itself doesn’t manage your dependencies for you, the plugins do. So this is actually a comparison between two of the best plugins I’ve found.

usemin

Usemin allows you to add good old script tags to your html, and html comments to figure out where to put the combined file. This is pretty cool, but it’s only cool when using static html files as it rewrites your html for you. I was able to still implement this, have it write my views to a different directory and tell my app to use those views. This worked okay, but still felt a bit weird and dirty.

concat

grunt-contrib-concat, the best I’ve found. This plugin is really straight forward. Specify the javascript sources, and where (what file) you’d like to put them. It just concatenates a list of scripts, and allows you to supply wildcards, too (the way grunt does). This is super simple, and actually is all anyone would ever need. Downside? It requires quite a bit of extra work as you’ll always have to update the task in the gruntfile whenever you add a new dependency and also forces you to keep a mapping between those files and the gruntfile so you don’t end up with orphans.

In comes useuses

Tired of looking through all these alternatives I decided it was time to write something myself. I called it useuses because it allows you to use uses. Aren’t I creative?

What it is.

Before I started writing this plugin, I assembled a list of requirements:
* It must be optional.
* It mustn’t force a dependency upon someone.
* It must be “standard”, so no weird tags.
* It must add value to the files, even when not used.

With those requirements set, I came up with a way to specify what dependencies a file has, to make it informative for the developers. So as soon as you were to open up a file, you’d immediately see what it depends on. This made me think of the way jquery ui does that. It’s nice, but not nice enough. It’s still unformatted information.

The syntax

I finally came up with the following syntax:

This is informative and standardized in the docblock format. I immediately added support some other formats:

Conclusion

I liked all alternatives and think that the developers did a great job creating solutions. I think that whatever you decide to use depends on your project’s requirements and development style.

If you wish to give useuses a go, you can find the npm package for useuses by clicking here.

Happy coding.

PHP sandbox online editing

I’m guessing you got to this page because you like PHP, and you like testing without creating entire directory structures or using php -r ”;.

Well, me too. That’s why I went out trying to find a tool that could help me with exactly that. One problem! There were none. And the ones that I did find also did not work the way I wanted them to. They were either online, too bloated, or required ftp access.

So, I built one. Yes, yes I did. You can find the code here: https://github.com/RWOverdijk/SxPhandbox

Installation should be pretty straight forward, as explained in the readme.

Runtime errors in the editor

Have fun!

Validate existence of youtube video using javascript and jquery

There’s no need to include a description. The title says it all.

[2015-01-22] Update: The snippet has been updated to work with the new youtube urls (which may contain dashes).

©SpoonX 2017