Skip to content

shaunxcode/chain-of-strength

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Chain Of Strength

What is this? Essentially I needed a way of serializing js statements to pass to phantom js, meaning the functions can not depend on the ability to refer to any outside variables.

Consider (assuming you have a phantom js page object):

x = 6
y = 7
selector = ".div"

page.evaluate -> $(selector).addClass("#div-#{x}-#{y}")

This will fail as it refers to selector, x, and y.

Chain allows you to build a function returning a call chain:

COS = require "chain-of-strength"

x = 6
y = 7
selector = ".div"

page.evaluate COS.func $: selector, addClass: "#div-#{x}-#{y}"

Obviously this is a little more noisy, but definitely nicer than

page.evaluate "function(){ return $('#{selector}').addClass('#div-#{x}-#{y}') }"

You can also just get a raw method chain w/o a function wrapper by calling buildChain

COS.buildChain ['a', b: [1,2]]

#yields

"a.b(1,2)"

There are times when you may want to have a nested callback e.g. to a map/filter method

COS.func $: selector, map: func: ["$(this)", css: "display"]

#yields

'function(){return $(".div").map(function(){return $(this).css("display")})}'

About

chain-of-strength npm package for building method chain strings from array based dsl - useful for passing in functions to phantom js

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published