How to reference a plain object in another file with JSdoc? - node.js

I want to reference a variable to a plain object from another file via JSDoc to use IDE autocomplete.
Here is my example:
MySample.js
export const MySample = {
findMe() {
return "Hi!";
},
};
export default MySample;
index.js
import MySample from "./MySample.js";
let window = {};
function inject($key, $object) {
window[$key] = $object;
}
inject("MySample", MySample);
let my_sample = window.MySample;
console.log(my_sample.findMe());
I tried to use something like the following, but it was not successful:
/**
* #type {import('./MySample').MySample} my_sample
*/
P.S: I know if My Sample be a class, the above solution is working. But in my scenario I want to reference a plain object.

I found the solution as follows:
/**
* #param {import('./MySample').MySample} my_sample
*/
inject("MySample", MySample);
The key is using #param instead of #type

Related

On an afterSubmit when we creating a copy of one inventory item (name with '-c') ,The original ID of item link should come in a field on a copy order

I tried this above, here I am getting a null value only from my previous record.
Kindly give some guidance to solve my questions.
thanks in advance.
/**
*#NApiVersion 2.0
*#NScriptType UserEventScript
*/
define(["N/url", "N/record", "N/runtime"], function (url, record, runtime) {
function afterSubmit(context){
var recordobj = context.newRecord;
var prevItemrecord= context.oldRecord;
var Itemname = recordobj.getValue({fieldId:'itemid'});
var prevItemname = prevItemrecord.getValue({fieldId : 'itemid'});
var Type=context.type;
var checkbox=recordobj.getValue({fieldId:'custitem17'});
if(Type== context.UserEventType.CREATE)
if((Itemname=prevItemname+'-c')&&(checkbox=true))
record.submitFields({
type: recordobj.type,
id: recordobj.id,
values:{custitem_item_link:prevItemname}
});
}
return{
afterSubmit:afterSubmit
}
});
This is my code
On create there is no old record.
Since you are trying to update the same record as was just created you are better off having this in a beforeSubmit event script
if((Itemname=prevItemname+'-c')&&(checkbox=true)) this is an error
if((Itemname == prevItemname+'-c') && checkbox) is more what you need
If you are trying to capture a copy operation you can set that up in the beforeLoad event that you use in the beforeSubmit event.
function beforeLoad(ctx){
if(ctx.type == ctx.UserEventType.COPY){
if(ctx.form){
ctx.form.addField({
id:'custpage_original_item',
label:'Copied Item',
type:ui.FieldType.SELECT,
source:'item'
}).updateDisplayType({
displayType:ui.FieldDisplayType.HIDDEN
}).defaultValue = ctx.request.parameters.id;
// your naming makes me wonder if you are trying to link to the
// source item rather than just saving a reference to the source
// item's name
/*
* Using the original item's name like below is closer to what you
* posted but I think by the time the script runs the itemid field
* has been cleared.
* ctx.form.addField({
* id:'custpage_original_item_name',
* label:'Copied Item Name',
* type:ui.FieldType.TEXT
* }).updateDisplayType({
* displayType:ui.FieldDisplayType.HIDDEN
* }).defaultValue = ctx.newRecord.getValue({fieldId:'itemid'});
*/
}
}
}
function beforeSubmit(ctx){
if(ctx.type == ctx.UserEventType.CREATE){
const itemRec = ctx.newRecord;
if(itemRec.getValue({fieldId:'custitem17'})){
const sourceId = itemRec.getValue({fieldId:'custpage_original_item'})
if(sourceId){
itemRec.setValue({
fieldId:'custitem_item_link:prevItemname',
value:sourceId
});
/* or use a search function to look up the original item's name
* and then test the new item's name.
*/
}
}
}
}

Get list of version ids for a blob using node.js

I am trying to get list of version ids for a blob using node.js.
async function getBlobNameAndVersions() {
for await (const blob of containerClient.listBlobsFlat(includeVersions?: true)) {
const tempBlockBlobClient = containerClient.getBlockBlobClient(blob.name);
console.log(`\n\tname: ${blob.versionId}\n\tURL: ${tempBlockBlobClient.name}\n`);
}
}
getBlobNameAndVersions()
Received error saying "ReferenceError: inlcudeVersions is not defined"
When looked at the reference listBlobsFlat, below were options to include versions.
listBlobsFlat(options?: ContainerListBlobsOptions): PagedAsyncIterableIterator<BlobItem, ContainerListBlobFlatSegmentResponse>;
/**
* Returns an AsyncIterableIterator for ContainerListBlobHierarchySegmentResponse
*
* #param delimiter - The character or string used to define the virtual hierarchy
* #param marker - A string value that identifies the portion of
* the list of blobs to be returned with the next listing operation. The
* operation returns the ContinuationToken value within the response body if the
* listing operation did not return all blobs remaining to be listed
* with the current page. The ContinuationToken value can be used as the value for
* the marker parameter in a subsequent call to request the next page of list
* items. The marker value is opaque to the client.
* #param options - Options to list blobs operation.
Tried using options as below.
export declare interface ContainerListBlobsOptions extends CommonOptions {
* Specifies whether versions should be included in the enumeration. Versions are listed from oldest to newest in the response.
*/
includeVersions?: boolean;
}
You would need to pass the options as an object i.e. instead of passing includeVersions?: true, you would need to use {includeVersions: true}.
So your code would be something like:
async function getBlobNameAndVersions() {
for await (const blob of containerClient.listBlobsFlat({includeVersions: true})) {
const tempBlockBlobClient = containerClient.getBlockBlobClient(blob.name);
console.log(`\n\tname: ${blob.name}\n\tURL: ${tempBlockBlobClient.url}\n`);
}
}
Updated Code is working to get version ids of blobs and its URL's
async function getBlobNameAndVersions() {
for await (const blob of containerClient.listBlobsFlat({includeVersions: true})){
const tempBlockBlobClient = containerClient.getBlockBlobClient(blob.name);
console.log(`\n\tversion: ${blob.versionId}\n\tURL: ${tempBlockBlobClient.url}\n`);
}
}

How to extend Tabulator with resizable column plugin

I thought this would be shown in the docs but I'm trying to get column resizing working with Tabulator.
This page just describes how to import the plugin:
import {ResizeTableModule} from 'tabulator-tables';
But what do you with this import? I thought you could add it to Tabulator.extendModule(ResizeTableModule) but that does nothing. I couldn't find any examples using plugins modularly like this. Thanks for the help.
To get column resizing working, you can import ResizeColumnsModule as follows:
import { Tabulator, ResizeColumnsModule } from "tabulator-tables";
Tabulator.registerModule(ResizeColumnsModule)
Example: https://codesandbox.io/s/gifted-wescoff-nw337l?file=/src/index.js
As per docs correct way to extend a module is like
/**
* #param {String} moduleName
* #param {any} configs
* #param {any} newconfig
*/
Tabulator.extendModule("resizeTable", "config", {
//Your New Config
name: "__int__",
});
I am afriad as ResizeTableModule module does not expose any other config but you can define them by extending the class
import { Tabulator, ResizeColumnsModule, ResizeTableModule } from "tabulator-tables";
class MyResizeTableModule extends ResizeTableModule {
constructor(table) {
super(table);
this.config = MyResizeTableModule.config;
}
}
MyResizeTableModule.config = {};
Tabulator.registerModule([MyResizeTableModule, ResizeColumnsModule]);
/**
* #param {String} moduleName
* #param {any} configs
* #param {any} newconfig
*/
Tabulator.extendModule("resizeTable", "config", {
//Your New Config
name: "__int__",
});
See Codesandbox

What's the "official" way to pass variables in SS2.0?

Run into a kind of silly problem.
I want to pass a variable between stages on a map/reduce script. Is it there an "official" or best way to do this (rather than sending it with the returned results).
This is my last approach:
/**
* #NApiVersion 2.0
* #NScriptType MapReduceScript
*/
define(["N/search", "N/record", "N/email", "N/runtime", "N/task", "/SuiteScripts/Libraries/tools_lib"],
function (search, record, email, runtime, task, tools) {
var ss = runtime.getCurrentSession();
var conf = {};
/**
* Retrieve the CSV contents and return as an object
* #return {Array|*}
*/
function getInputData() {
log.debug("setting", "foo");
ss.set({name: "foo", value: "bar"});
//Session
var foo = ss.get({name: "foo"});
log.debug("foo 1", foo);
//var pass
conf["foo"] = "bar";
return [1, 2, 3];
}
/**
* Search and group by type, all records with matching entries on the CSV field
* #param context
* #return {boolean}
*/
function map(context) {
//Session
var foo = ss.get({name: "foo"});
log.debug("foo 2", foo);
//Var pass
log.debug("foo 3", conf["foo"]);
return false;
}
foo 1 = bar
foo2 = null
foo3 = null
NetSuite stores whatever you return from the previous phase in context.value.
No matter what data type you return, it will always get sent to the next phase as a String, so you need to JSON.parse it if you want to work with a different data type.
function getInputData() {
return [1,2,3];
}
function map(context) {
log.debug(JSON.parse(context.value));
}
You cannot get access to specific variables from previous phases. If you want to pass data along, you need to build an appropriate data structure with the values you want to pass and return it, then parse it out of context.value in the subsequent phase.
This is coming a little late, but one solution/workaround I found is using a Scheduled Script to call a Map/Reduce Script.
You can dynamically set script parameters within the Scheduled Script when you initiate the map/reduce script (using the 'N/task' module).

Raphaeljs: Copy element or set to another paper

Is there a possibility of adding an element or set that exists within a paper to another paper, without creating each element twice from scratch?
Background for this: I visualize a large node graph and want to create an "overview map" in a separate paper.
The following set of codes adds a nw function to Raphael Set and Raphael Elements. The usage is to simply call .cloneToPaper(targetPaper) on any set or element.
(function (R) {
var cloneSet; // to cache set cloning function for optimisation
/**
* Clones Raphael element from one paper to another
*
* #param {Paper} targetPaper is the paper to which this element
* has to be cloned
*
* #return RaphaelElement
*/
R.el.cloneToPaper = function (targetPaper) {
return (!this.removed &&
targetPaper[this.type]().attr(this.attr()));
};
/**
* Clones Raphael Set from one paper to another
*
* #param {Paper} targetPaper is the paper to which this element
* has to be cloned
*
* #return RaphaelSet
*/
R.st.cloneToPaper = function (targetPaper) {
targetPaper.setStart();
this.forEach(cloneSet || (cloneSet = function (el) {
el.cloneToPaper(targetPaper);
}));
return targetPaper.setFinish();
};
}(Raphael));
For a sample implementation, you may check out this fiddle: http://jsfiddle.net/shamasis/39yTS/
Note that if you have events on the source elements, they will not be cloned to the target paper.
Raphael don't allow to move element from one paper to another directly.
So it is better to create a new element with same property in the target paper.
I have created following sample method. you can add the code in your page and use cloneToPaper function to clone a element or a set to another paper.
function extractJSON(element) {
var attr = element.attr(),
newNodeJSON = {type: element.type},
key;
for (key in attr) {
newNodeJSON[key] = attr[key];
}
return newNodeJSON;
}
/*
* #param {Object} element: raphael element or set
* #param {Object} paper: Target paper where to clone
* #return {object}: newly created set or element
*/
function cloneToPaper(element, paper) {
var isSet = element.type === 'set',
elementJSONArr = [],
i, ln, newSet;
if (isSet) {
ln = element.items.length;
for (i = 0; i < ln; i += 1) {
elementJSONArr.push(extractJSON(element.items[i]));
}
}
else {
elementJSONArr.push(extractJSON(element));
}
newSet = paper.add(elementJSONArr);
return isSet ? newSet : newSet[0];
}

Resources