Giter VIP home page Giter VIP logo

Comments (4)

mbloch avatar mbloch commented on June 29, 2024

Hi!

I should be able to make some changes to support your use cases. Part of the problem may be that mapshaper is using import() instead of require() to load external modules. import() can load node-style modules, but (as far as I understand) doesn't search the same set of paths as require(), which explains why modules installed locally with npm aren't loading. I could try using require() if import() fails.

I'll look into the global context issues you reported.

from mapshaper.

indus avatar indus commented on June 29, 2024

I investigated a bit further and i think the last bullet (no access to context) is just caused because the function is enforced to be a nested property of the alias and not on the global level.

with this script...

module.exports = {
    fnA:function(){
        console.log(Object.getOwnPropertyDescriptors(this))
    },
    nested:{
        fnB:function(){
            console.log(Object.getOwnPropertyDescriptors(this))
        }
    }
}
mapshaper -require "./index.js" -run "{fnA()}" -run "{nested.fnB()}"

... fnA has context in this and nested.fnB does not

At the moment functions in npm modules are always a nested properties. This make the difference?!

from mapshaper.

indus avatar indus commented on June 29, 2024

Just to give you some context. The functions I use most often generate geojson output based on geojson input. And the template I use looks something like this:

const fnName = "myFn"

module.exports = {
    [fnName]: function (arg1, arg2, arg3) {
        const { $, target } = this;

        if ($) {
            const geojson = $.geojson;

            // generate modified geojson

            return geojsonMOD;

        } else if (target) {

            const fnName_ = `[+ ${fnName}]`;

            console.info(`${fnName_} The target gets cloned and renamed - use '-each ${fnName}(...)' to alter it in place`)

            let args = [...arguments].map(JSON.stringify)

            let name = [target.layer_name, `_${fnName}`, ...args]

            return `-filter "true" target=${target.layer_name} + name=${name.join('_')}
            -each this.geojson=${fnName}(${args.join(',')})`;

        }
    }
}

This allows the function to be called with -run myFn(1,2,3) to create a new layer based on the current target as well as with -each this.geojson = myFn(1,2,3) to change the current target in place with the same arguments.

(maybe I should use the io api of -run to generate new layer instead of copying them with -filter "true"🤔 )

from mapshaper.

indus avatar indus commented on June 29, 2024

I've just tested how to make the context thing work. Turns out that If npm modules would also be asigned to global like so the context would be available with this. This would fix this issue as well.

Maybe the functions in the module should be assigned to global by default and only should nested if alias is used

from mapshaper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.