FluentNode
FluentNode is a set of Node.js Javascript APIs that support functional programming:
- designed to extent core Javascript/Node objects,
- represents a continuous effort of simplifying complex actions, in order to allow the easy development and testing of readable code
- written in Literate Coffee-Script (using TDD)
- 100% code coverage (on both code and tests)
- based on fluent APIs
- an Javascript implementation of FluentSharp (part of OWASP O2 Platform)
@Fogus Functional Javascript book contains a description which completely matches the philosophy behind FluentNode.
In page 25, Functional programming is defined as consisting of the following techniques:
- Identifying an abstraction and building a function for it
- Using existing functions to build more complex abstractions
- Passing existing functions to other functions to build even more complex abstractions
Install on Node.js
npm install fluentnode --save
Documentation and FluentNode APIs
The code is written in literate coffee-script where the documentation is part of the sourcecode.
See http://o2platform.com/fluentnode for the list of available functions, annotated source code, and all tests (site created using docco)
if you are viewing this in GitHub, the individual source code files (linked below) look quite nice (due to GH's support for literate Coffee-Script):
Examples
To show FluentNode in action, here are some examples taken from FluentNode's UnitTests
String::folder_Create and String::folder_Delete (in fs.test.coffee)
it 'folder_Create and folder_Delete' -> # tests the String's folder_Create and folder_Delete methods ""folder_Createassert_Is_Function # checks if String::folder_Create exists ""folder_Deleteassert_Is_Function # checks if String::folder_Delete exists tmpDir = "./"temp_Name_In_Folder # get a temp folder name in the folder provided as string tmpDirfolder_Existsassert_Is_False # checks if the folder exists (expects it to be false) tmpDirfolder_Createassert_Is tmpDirrealPath # creates folder and confirms that the return value is the full path to the folder tmpDirfolder_Existsassert_Is_True # confirms that folders exists tmpDirfolder_Deleteassert_Is_True # deletes folder (confirming OK result from delete action) tmpDirfolder_Existsassert_Is_False # confirms that doesn't exists
There are actually two asserts that can be used to simply this code:
tmpDirassert_Folder_Not_Exists # asserts that folder doesn't exist folder_Createassert_Is tmpDirrealPath # creates folder and confirms that the return value is the full path to the folder tmpDirassert_Folder_Exists # assert that folders exists folder_Deleteassert_Is_True # deletes folder (confirming OK result from delete action)
String::file_Contents (in fs.test.coffee)
it 'file_Contents' -> ''file_Contentsassert_Is_Function file_Name = '_temp_name_'add_Random_String5 file_Contents = 'value_'add_Random_String5 file_Namefile_Existsassert_Is_False file_ContentssaveAsfile_Name file_Namefile_Existsassert_Is_True file_Namefile_Contentsassert_Isfile_Contents file_Namefile_Deleteassert_Is_True
String::files (in fs.test.coffee)
it 'files' -> ''filesassert_Is_Function files = './'filesfilter file isnt '.DS_Store'realPath expectedFiles = filerealPath for file in '.gitignore,.travis.yml,LICENSE,README.md,package.json'split',' filesassert_ContainsexpectedFiles './'files'.yml' assert_Is'.travis.yml'realPath './'files'.json'assert_Is'package.json'realPath
String::http_Status (in http.test.coffee)
it 'http_Status' ''http_Statusassert_Is_Function urlhttp_Status statusassert_Is200 done
String::http_Get (in http.test.coffee)
it 'http_GET' ''http_Statusassert_Is_Function req = urlhttp_GET assert_Is_Nullerr dataassert_Is_String reqassert_Instance_OfhttpClientRequest resassert_Instance_OfhttpIncomingMessage dataassert_Istest_Data done
String::start_Process_Redirect_Console (in process.test.coffee)
it 'start_Process_Redirect_Console' original_log = consolelog log_Messages = = log_MessagespushlogMsg childProcess = 'ls'start_Process_Redirect_Console'.' childProcesson 'exit'-> consolelog 'process ended' log_Messagesfirst assert_Contains'README.md' log_Messagessecondassert_Is'process ended' console.log = original_log done
Contribute
See the current Issues list for a good place to start
If you need a function that is currently missing, please fork this repo and send a PR with the implementation and tests :)
Projects using FluentNode
- TeamMentor 4.0 GraphDB - https://github.com/TeamMentor/TM_4_0_GraphDB