using Underscore in SuiteScript 2 - netsuite

I am writing a script in SS2 and I ran into doc that stated that I should do the below. I have done this and the paths are right but it doesn't seem to see underscore as it comes back as undefined, when trying to use it. Any help with this would be great, thanks
define( [ 'N/email'
, 'N/runtime'
, 'N/search'
, '/SuiteScripts/af_scripts/underscore.js#1.8.3/underscore'
],
function( email, runtime, search, _) {
function onRequest( context ) {
var request = context.request;
var us = _;
}
return {
onRequest: onRequest
};
} );

In my case, I used lodash to add HTML file contents to my suitelet
When you define your modules you can insert the lodash library at the end just like
define(['N/file', 'N/record', 'N/search', 'N/ui/serverWidget','./lodash.js'],
but at function you should not insert anything just like
function(file, record, search, serverWidget) {
Here is a sample of code to load a file and get his contents using lodash
/**
* #NApiVersion 2.x
* #NScriptType Suitelet
* #NModuleScope SameAccount
*/
define(['N/file', 'N/record', 'N/search', 'N/ui/serverWidget','./lodash.js'],
/**
* #param {file} file
* #param {record} record
* #param {search} search
* #param {serverWidget} serverWidget
*/
function(file, record, search, serverWidget) {
/**
* Definition of the Suitelet script trigger point.
*
* #param {Object} context
* #param {ServerRequest} context.request - Encapsulation of the incoming request
* #param {ServerResponse} context.response - Encapsulation of the Suitelet response
* #Since 2015.2
*/
function onRequest(context) {
var templateFile = file.load({
id: 'filePath'
});
//for example.
var compiled = _.template(templateFile.getContents());
}
return {
onRequest: onRequest
};
});
Note:
I inserted the file to the same location as my suitelet in the file cabinet that why I used this relative path (./lodash.js), use the full path if the suitelet is not at the same file.

I have not see how to do it that way. But the way I found that works is like this:
/**
* #NApiVersion 2.x
* #NScriptType usereventscript
*/
require.config({
paths:{
"coolthing":"/SuiteScripts/myFavoriteJsLibrary"
}
});
define(['coolthing'],function (coolthing){
return {
beforeLoad:function beforeLoad(ctx){
coolthing.times(2,function(){
log.debug('log','log');
});
}
};
});

I put underscore-min.js in the same folder as my Suitescripts.
Then I created a config file named 'underscoreConfig.json' in the same folder with the following contents:
{
"paths":{
"underscore": "./underscore-min"
}
}
Then I add the following to my scripts:
/**
* #NApiVersion 2.x
* #NScriptType UserEventScript
* #NModuleScope SameAccount
* #NAmdConfig ./underscoreConfig.json
*/
define(['underscore', 'N/record', 'N/search'],
/**
* #param {underscore} underscore
* #param {record} record
* #param {search} search
*/
function(_, record, search) {
Now I am able to call Underscore functions in my scripts.
E.g.
var segmentObj = _.findWhere(segmentFields, {id: segmentId});

Related

puppeteer files from lib folder not exist on server side install

On my windows machine I got a lot of files in my lib folder:
./node_modules/puppeteer/lib
However when I install puppeteer on my ubuntu server (Ubuntu 20.04 LTS) all of those files does not exist. There is only a cjs & esm folder.
I got a custom function in my script which access the lib files like this:
// extract the Page class
const { Page } = require("puppeteer/lib/Page");
/**
* #name elementContains
* #param {String} selector specific selector globally search and match
* #param {String} text filter the elements with the specified text
* #returns {Promise} elementHandle
*/
Page.prototype.elementContains = function elementContains(...args) {
return this.evaluateHandle((selector, text) => {
// get all selectors for this specific selector
const elements = [...document.querySelectorAll(selector)];
// find element by text
const results = elements.filter(element => element.innerText.includes(text));
// get the last element because that's how querySelectorAll serializes the result
return results[results.length-1];
}, ...args);
};
/**
* Replicate the .get function
* gets an element from the executionContext
* #param {String} selector
* #returns {Promise}
*/
const { JSHandle } = require("puppeteer/lib/JSHandle");
JSHandle.prototype.get = function get(selector) {
// get the context and evaluate inside
return this._context.evaluateHandle(
(element, selector) => {
return element.querySelector(selector);
},
// pass the JSHandle which is itself
this,
selector
);
};
Everything works fine on my windows machine. However on my ubuntu server it does not work because those files does not exist. I manually uploaded all files into this folder from my windows machine however I get then:
-TypeError: page.elementContains is not a function
Can you help me please why does the lib folder doesn´t contain the needed files on linux?

How to call a function inside webdriverio test configuration file

The url shows a WebdriverIO test runner configuration
https://webdriver.io/docs/configurationfile.html
It has got many hooks. Consider the hook onComplete I want to write a function, may be a function to create a file. In another file and call that function inside the onComplete hook. Could you please help me to achieve this.
Yes, you pretty much described the flow.
Define your function in a file and export it:
module.exports = (() => {
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* > Def: Polls the DOM until the given 'desiredState' is found.
* #param {string} desiredState ['loading', 'interactive', 'complete']
* #returns {Promise} WebdriverIO Promise
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
browser.addCommand('waitForReadyState', (desiredState) => {
let foundState;
browser.waitUntil(() => {
foundState = browser.execute('return document.readyState;');
console.log(`\n> Waiting for page to load ... | Current state: '${foundState}'`);
return foundState === desiredState;
}, browser.options.waitforTimeout, `Timeout before expected state! Found: '${foundState}' | Expected: '${desiredState}'`);
});
})();
Then, import it in the desired hook (e.g: for a custom_command, the before hook):
before: function (capabilities, specs) {
require('./test/custom_commands/waitForReadyState');
}
You can easily reproduce the model to implement logging & file manipulation functions that you need to run in the onComplete hook.
its maybe late but here is how you can do it :
/** file to keep your function should be in es5 or you have to add babel to covert it to es6 before WebdriverIO start **/
test.js
module.exports = function foo(){
console.log('here');
}
in config file //before export.config:
const foo = require('path-to-test.js');
use foo() in onComplete() hook

Is this a good way to use module.exports?

I'm curious if the following way is a good approach to use the module.exports functionality.
And if so, what is the best way to add commentary to the code so JSDoc3 can handle it correctly.
I'm currently exporting functions as follows:
Let say we have a module1.js with the following code:
/**
* Module1 module
* #module Module1
*/
module.exports = {
// no jsdoc commentary on purpose
function1: function(param1) {
function1(param1);
},
/**
* Function2
*
* Function that will do something
* #returns {boolean} return value
*/
function2: function() {
return function2();
},
}
/**
* Function1
*
* Function that will do something
* #param param1 {string} First parameter
*/
function function1(param1){
console.log(param1);
// do something
}
/**
* Function2
*
* Function that will do something
* #returns {boolean} return value
*/
function function2() {
// do something
return true;
}
and in module2.js
/**
* Module2 module
* #module Module2
*/
// Requirements
module1 = require('./module1.js');
/**
* myFunction
*
* Function that will do something
*/
function myFunction() {
const param1 = "Parameter1";
module1.function1(param1); // call function1 from module 1
}
// just an example of function2 in module2.js
const myBoolean = module1.function2();
// more stuf
The problem with this approach is that function1 will now only be described in the jsdoc file as 'inner'(local) function, and function2will now be duplicated in the generated jsdoc file one as 'static' (the exported) and one as 'inner' (local) function.
Why this approach:
The reason for this approach is: I want no functionality in exported functions. I want other developers to easily see what functions are exported. So the real functionality is put in the local function below the exports.
The question:
Is this a correct way to use the module exports?
And is this a correct way to commentary the code? (for JSDoc3)
ps: im pretty new with NodeJS

JSDoc when exporting a function constructor in node.js

I'm just starting to add some JSDoc comments to a code base I've been working on, for the most part this seems to work, but there's one area that's causing me some difficulties.
If I describe a function constructor in a file, and then export it on the module.exports, when I later require() that module, I get no documentation on the type and the inferred type is set as |exports. Clicking on the doesn't take me anywhere. I currently have:
/**
* Creates an instance of the StatusCodeErrorItem
* #param {string} message The message for this error
* #param {object} params The parameters that caused this error to occur
* #alias lib/common/StatusCodeErrorItem.StatusCodeErrorItem
* #returns {StatusCodeErrorItem}
* #constructor
*/
function StatusCodeErrorItem(message, params) {
this.message = message;
this.params = params;
}
/**
* Holds information about an error
* #module lib/common/StatusCodeErrorItem
*/
module.exports = StatusCodeErrorItem;
and in the file that uses it:
var StatusCodeErrorItem = require('./StatusCodeErrorItem');
I'd thought at this point that'd I'd be able to press f1 to bring up the inline documentation, and see the definition of StatusCodeErrorItem as described in that file. But instead I only see: inferred type StatusCodeErrorItem|exports
Webstorm 9
Node.js 0.10.36 (I know both are old)
Any thoughts on what I'm doing wrong? Is what I'm after even possible? Are my versions simply too old?
Cheers
Webstorm 2016.1.1 solves the issue. The below JSDoc gets correctly assigned to types pulled in by require. I'll now be pestering people to get me a new license.
'use strict';
/**
* Creates an instance of the StatusCodeErrorItem
* #memberof common
* #constructor
* #classdesc A class for holding information about an error. The params object allows the tracking of the function
* parameters that caused the error, but should not be used to store large objects.
* #description Creates an instance of the StatusCodeErrorItem with the given message and optional params object
* #param {string} message The message for this error
* #param {object} [params] The parameters that caused this error to occur
* #returns {StatusCodeErrorItem}
* #see {#link module:lib/common/StatusCodeError.StatusCodeError|StatusCodeError}
*/
function StatusCodeErrorItem(message, params) {
this.message = message;
this.params = params;
}
/**
* Surfaces a class that holds information about an error
* #module lib/common/StatusCodeErrorItem
*/
module.exports = StatusCodeErrorItem;

Working example of event tag in jsdoc3

Can someone please give me an example on how to use the #event tag properly.
I've looked at the jsdoc3 documentation here:
http://usejsdoc.org/tags-event.html
but I could not get it working.
I am using nodejs, and I have a module in MyClass/index.js which exports a constructor by the name of MyClass:
module.exports = MyClass;
myClass = function () { ... };
and I have tried to add
/**
* Snowball event.
* #event MyClass#snowball
*/
In the compiled docs, snowball appears in the global list of events, but when I click on it, it tells me
The requested URL /{path-to-my-docs}/MyClass.html was not found on this server.
Thanks in advance.
This works fine:
/**
* #class
*/
function Blah() {
}
/**
* #event Blah#something
* #type {Object}
* #property {integer} some_field Represents something or other.
*/

Resources